Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PatternTest.php « Filter « DataTable « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 19f8c51d52c1e0ae539c03884faf40a0af4504f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Unit\DataTable\Filter;

use Piwik\DataTable;
use Piwik\DataTable\Row;

/**
 * @group DataTableTest
 * @group Core
 */
class PatternTest extends \PHPUnit\Framework\TestCase
{
    /**
     * Dataprovider for testFilterPattern
     */
    public function getTestData()
    {
        return array(
            array('ask', array(1)),
            array('oo', array(0, 3)),
            array('^yah', array(3)),
            array('\*', array(6)),
            array('2/4', array(5)),
            array('amazon|yahoo', array(3, 4)),
        );
    }

    /**
     * Test to filter a column with a pattern
     *
     * _Pattern
     * @dataProvider getTestData
     */
    public function testFilterPattern($pattern, $expectedRows)
    {
        $table = new DataTable;

        $idcol = Row::COLUMNS;

        $rows = array(
            array($idcol => array('label' => 'google')),
            array($idcol => array('label' => 'ask')),
            array($idcol => array('label' => 'piwik')),
            array($idcol => array('label' => 'yahoo')),
            array(Row::METADATA => array('label' => 'amazon')),
            array($idcol => array('label' => '2389752/47578949')),
            array($idcol => array('label' => 'Q*(%&*("$&%*(&"$*")"))'))
        );
        $table->addRowsFromArray($rows);
        $rowIds = array_keys($rows);

        $rowToDelete = array_diff($rowIds, $expectedRows);
        $expectedtable = clone $table;
        $expectedtable->deleteRows($rowToDelete);
        $filteredTable = clone $table;
        $filteredTable->filter('Pattern', array('label', $pattern));
        $this->assertEquals($expectedtable->getRows(), $filteredTable->getRows());
    }

    /**
     * @dataProvider getTestData
     */
    public function testFilterArrayPattern_OneColumn($pattern, $expectedRows)
    {
        $rows = array(
            array('label' => 'google'),
            array('label' => 'ask'),
            array('label' => 'piwik'),
            array('label' => 'yahoo'),
            array('label' => 'amazon'),
            array('label' => '2389752/47578949'),
            array('label' => 'Q*(%&*("$&%*(&"$*")"))')
        );

        $filter = new DataTable\Filter\Pattern(new DataTable(), array('label'), $pattern);
        $filteredRows = $filter->filterArray($rows);

        $this->assertSame($expectedRows, array_keys($filteredRows));
    }

    /**
     * @dataProvider getTestDataTwoColumns
     */
    public function testFilterArrayPattern_ShouldBeAbleToFilterByTwoColumns_IfMultipleColumnsAreGiven($pattern, $expectedRows)
    {
        $rows = array(
            0 => array('randomcolumn' => 'ask', 'name' => 'google', 'url' => 'www.google.com'),
            1 => array('randomcolumn' => 'goo', 'name' => 'ask', 'url' => 'www.ask.com'),
            2 => array('randomcolumn' => 'com', 'name' => 'piwik', 'url' => 'piwik.org'),
            3 => array('randomcolumn' => 'com', 'name' => 'yahoo', 'url' => 'nz.yahoo.com'),
            4 => array('randomcolumn' => 'nz1', 'name' => 'amazon', 'url' => 'amazon.com'),
            5 => array('randomcolumn' => 'com', 'url' => 'nz.piwik.org'),
            6 => array('randomcolumn' => 'com', 'name' => 'Piwik')
        );

        $filter = new DataTable\Filter\Pattern(new DataTable(), array('name', 'url'), $pattern);
        $filteredRows = $filter->filterArray($rows);

        $this->assertSame($expectedRows, array_keys($filteredRows));
    }

    /**
     * Dataprovider for testFilterPattern
     */
    public function getTestDataTwoColumns()
    {
        return array(
            array('ask', array(1)),
            array('oo', array(0, 3)),
            array('^yah', array(3)),
            array('nz', array(3, 5)),
            array('amazon|yahoo', array(3, 4)),
            array('piwik', array(2, 5, 6)),
            array('com', array(0, 1, 3, 4)),
            array('org', array(2, 5)),
        );
    }
}