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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
<?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 AddSegmentByLabelTest
* @group DataTable
* @group Filter
*/
class AddSegmentByLabelTest extends \PHPUnit\Framework\TestCase
{
private $filter = 'AddSegmentByLabel';
/**
* @var DataTable
*/
private $table;
public function setUp()
{
$this->table = new DataTable();
$this->addRow(array('label' => 'http://piwik.org/test'));
$this->addRow(array('label' => ''));
$this->addRow(array('label' => 'search test', 'other' => 'value'));
$this->addRow(array('label' => 'keyword t/est'));
$this->addRow(array('label' => false));
$this->addRow(array('label' => 'play A movie', 'other' => 'value'));
$this->addRow(array('nb_visits' => 15));
$this->addRow(array('label' => 'Piwik'));
}
private function addRow($columns)
{
$this->table->addRow($this->buildRow($columns));
}
private function buildRow($columns)
{
return new Row(array(Row::COLUMNS => $columns));
}
public function test_filter_IfOnlyOneSegmentGiven_ShouldCopyTheValuePlain_IfOnlyOneSegmentIsGiven()
{
$segmentName = 'city';
$segmentStart = $segmentName . '==';
$this->table->filter($this->filter, array($segmentName));
$segmentValues = $this->table->getRowsMetadata('segment');
$expected = array(
$segmentStart . 'http%3A%2F%2Fpiwik.org%2Ftest',
false, // empty label we do not generate for this currently
$segmentStart . 'search+test',
$segmentStart . 'keyword+t%2Fest',
false,
$segmentStart . 'play+A+movie',
false,
$segmentStart . 'Piwik');
$this->assertSame($expected, $segmentValues);
}
public function test_filter_IfOnlyOneSegmentGiven_ShouldIgnoreASummaryRow()
{
$summaryRow = $this->buildRow(array('label' => 'mytest'));
$this->table->addSummaryRow($summaryRow);
$this->table->filter($this->filter, array('mysegment'));
$this->assertFalse($summaryRow->getMetadata('segment'));
}
public function test_filter_IfTwoSegmentsAreGiven_ShouldOnlyGenerateAFilterForLabelsHavingThatManyExplodedParts()
{
// must result in 2 exploded parts for city and region
$this->table->filter($this->filter, array(array('city', 'region'), $delimiter = ' '));
$segmentValues = $this->table->getRowsMetadata('segment');
$expected = array(
false,
false,
'city==search;region==test',
'city==keyword;region==t%2Fest',
false,
false,
false,
false);
$this->assertSame($expected, $segmentValues);
}
public function test_filter_IfMultipleSegmentsAreGiven_ShouldOnlyGenerateAFilterForLabelsHavingThatManyExplodedParts()
{
// must result in 3 exploded parts city, region and country
$this->table->filter($this->filter, array(array('city', 'region', 'country'), $delimiter = ' '));
$segmentValues = $this->table->getRowsMetadata('segment');
$expected = array(
false,
false,
false,
false,
false,
'city==play;region==A;country==movie',
false,
false);
$this->assertSame($expected, $segmentValues);
}
public function test_filter_IfMultipleSegmentsAreGiven_IfShouldBePossibleToIgnorePartsByUsingAnEmptyStringAsSegmentName()
{
// must result in 3 exploded parts city, region and country
$this->table->filter($this->filter, array(array('city', '', 'country'), $delimiter = ' '));
$segmentValues = $this->table->getRowsMetadata('segment');
$expected = array(
false,
false,
false,
false,
false,
'city==play;country==movie',
false,
false);
$this->assertSame($expected, $segmentValues);
}
public function test_filter_IfMultipleSegmentsAreGiven_ShouldIgnoreASummaryRow()
{
$summaryRow = $this->buildRow(array('label' => 'part1 part2'));
$this->table->addSummaryRow($summaryRow);
$this->table->filter($this->filter, array(array('seg1', 'seg2'), $delimiter = ' '));
$this->assertFalse($summaryRow->getMetadata('segment'));
}
}
|