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

ColumnCallbackDeleteMetadataTest.php « Filter « DataTable « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cf2258715c4993961ee2fe028fd2951a2fcaf9e8 (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
<?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 ColumnCallbackDeleteMetadataTest
 * @group DataTable
 * @group Filter
 */
class ColumnCallbackDeleteMetadataTest extends \PHPUnit\Framework\TestCase
{
    private $filter = 'ColumnCallbackDeleteMetadata';

    /**
     * @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'));
    }

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

    private function addRowWithMetadata($metadata)
    {
        $row = $this->buildRowWithMetadata($metadata);
        $this->table->addRow($row);

        return $row;
    }

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

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

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

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

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

        $metadata = $this->table->getRowsMetadata('test');
        $expected = array('1', '2', '3', '1', '4');
        $this->assertSame($expected, $metadata);
    }

    public function test_filter_shouldRemoveTheMetadataFromSubtables_IfOneIsSet()
    {
        $row   = $this->addRowWithMetadata(array('test' => '5', 'other' => 'value2'));
        $table = new DataTable();
        $table->addRow($this->buildRowWithMetadata(array('other' => 'value3')));
        $table->addRow($this->buildRowWithMetadata(array('test' => '6')));
        $table->addRow($this->buildRowWithMetadata(array('test' => '7', 'other' => 'value4')));
        $row->setSubtable($table);

        $this->table->filter($this->filter, array('other'));

        $this->assertFalse($row->getMetadata('other'));

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