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

PrependValueToMetadataTest.php « Filter « DataTable « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d26799a3d4fcd5b7a6c86927c8712e723197145 (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
<?php
/**
 * Piwik - 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\Core\DataTable\Filter;

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

/**
 * @group PrependValueToMetadataTest
 * @group DataTable
 * @group Filter
 */
class PrependValueToMetadataTest extends \PHPUnit\Framework\TestCase
{
    private $filter = 'PrependValueToMetadata';

    /**
     * @var DataTable
     */
    private $table;

    public function setUp(): void
    {
        $this->table = new DataTable();
        $this->addRowWithMetadata(array('test' => '1'));
        $this->addRowWithMetadata(array('test' => '2', 'other' => 'value'));
        $this->addRowWithMetadata(array('test' => '3'));
        $this->addRowWithMetadata(array('test' => '1', 'other' => 'value'));
        $this->addRowWithMetadata(array('test' => '4', 'other' => ''));
    }

    private function addRowWithMetadata($metadata)
    {
        $row = new Row(array(Row::COLUMNS => array('label' => 'val1')));
        foreach ($metadata as $name => $value) {
            $row->setMetadata($name, $value);
        }
        $this->table->addRow($row);
    }

    public function test_filter_shouldNotFailIfColumnOrValueIsNotSetOrDoesNotMatch()
    {
        $this->table->filter($this->filter, array('test', ''));
        $this->table->filter($this->filter, array('', 'test'));
        $this->table->filter($this->filter, array('', ''));
        $this->table->filter($this->filter, array('anyrandomcolumns', 'test'));

        // verify not modified
        $metadata = $this->table->getRowsMetadata('test');
        $this->assertSame(array('1', '2', '3', '1', '4'), $metadata);

        $metadata = $this->table->getRowsMetadata('other');
        $this->assertSame(array(false, 'value', false, 'value', ''), $metadata);
    }

    public function test_filter_shouldPrependValueToMetadataName_IfPossible()
    {
        $this->table->filter($this->filter, array('test', 'piwik_'));

        $metadata = $this->table->getRowsMetadata('test');
        $this->assertSame(array('piwik_1', 'piwik_2', 'piwik_3', 'piwik_1', 'piwik_4'), $metadata);

        // those should still be the same
        $metadata = $this->table->getRowsMetadata('other');
        $this->assertSame(array(false, 'value', false, 'value', ''), $metadata);
    }

    public function test_filter_shouldOnlyPrependIfAMetadataNameIsSet()
    {
        $this->table->filter($this->filter, array('other', 'prependme'));

        $metadata = $this->table->getRowsMetadata('other');
        $this->assertSame(array(false, 'prependmevalue', false, 'prependmevalue', 'prependme'), $metadata);

        // should still be the same
        $metadata = $this->table->getRowsMetadata('test');
        $this->assertSame(array('1', '2', '3', '1', '4'), $metadata);
    }
}