Skip to content
Extraits de code Groupes Projets
Valider a3d12964 rédigé par Thomas Steur's avatar Thomas Steur
Parcourir les fichiers

refs #4633 added tests for newly added filters

parent 8ef47c8e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -46,7 +46,7 @@ class AddSegmentFilterBySegmentValue extends BaseFilter
*/
public function filter($table)
{
if (empty($this->report) || empty($table) || !$table->getRowsCount()) {
if (empty($this->report) || !$table->getRowsCount()) {
return;
}
......
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.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;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
/**
* @group AddSegmentFilterByLabelMappingTest
* @group DataTable
* @group Filter
* @group Unit
* @group Core
*/
class AddSegmentFilterByLabelMappingTest extends UnitTestCase
{
private $filter = 'AddSegmentFilterByLabelMapping';
/**
* @var DataTable
*/
private $table;
public function setUp()
{
$this->table = new DataTable();
$this->addRow(array('label' => 1));
$this->addRow(array('label' => ''));
$this->addRow(array('label' => 3));
$this->addRow(array('label' => '4'));
$this->addRow(array('label' => 'play A movie', 'other' => 'value'));
$this->addRow(array('label' => 'Piwik'));
}
private function getEmptyMapping()
{
return array();
}
private function getMapping()
{
return array(
1 => 'Core',
2 => 'plugins',
3 => 'pluginstests'
);
}
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_shouldNotFail_IfMappingIsEmpty()
{
$this->table->filter($this->filter, array('segmentName', $this->getEmptyMapping()));
$metadata = $this->table->getRowsMetadata('segmentFilter');
$this->assertSame(array(false, false, false, false, false, false), $metadata);
}
public function test_filter_shouldMapOnlyValuesThatExistInMapping()
{
$this->table->filter($this->filter, array('segmentName', $this->getMapping()));
$metadata = $this->table->getRowsMetadata('segmentFilter');
$expected = array('segmentName==Core', false, 'segmentName==pluginstests', false, false, false);
$this->assertSame($expected, $metadata);
}
public function test_filter_shouldUrlEncodeValues()
{
$mapping = array(
1 => 'Core tests',
3 => 'plugins tästs'
);
$this->table->filter($this->filter, array('segmentName', $mapping));
$metadata = $this->table->getRowsMetadata('segmentFilter');
$expected = array('segmentName==Core+tests', false, 'segmentName==plugins+t%C3%A4sts', false, false, false);
$this->assertSame($expected, $metadata);
}
}
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.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;
use Piwik\Plugins\Actions\Reports\GetOutlinks;
use Piwik\Plugins\UserCountry\Reports\GetCity;
use Piwik\Plugins\UserCountry\Reports\GetCountry;
use Piwik\Plugins\VisitsSummary\Reports\Get;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
/**
* @group AddSegmentFilterBySegmentValueTest
* @group DataTable
* @group Filter
* @group Unit
* @group Core
*/
class AddSegmentFilterBySegmentValueTest extends UnitTestCase
{
private $filter = 'AddSegmentFilterBySegmentValue';
/**
* @var DataTable
*/
private $table;
private $report;
public function setUp()
{
$this->report = new GetCity();
$this->table = new DataTable();
$this->addRowWithMetadata(array('test' => '1'));
$this->addRowWithMetadata(array('test' => '2', 'segmentValue' => 'teeest'));
$this->addRowWithMetadata(array('test' => '3', 'segmentValue' => 'existing', 'segmentFilter' => 'city==mytest'));
$this->addRowWithMetadata(array('test' => '1', 'segmentValue' => 'test/test2.r'));
$this->addRowWithMetadata(array('test' => '4'));
}
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);
return $row;
}
public function test_filter_shouldGenerateASegmentFilterIfSegmentValueIsPresent()
{
$segmentValue = 'existing';
$expectedSegmentFilter = 'city==existing';
$this->assertSegmentFilterForSegmentValueAndReport($this->report, $segmentValue, $expectedSegmentFilter);
}
public function test_filter_shouldUrlEncodeTheValue()
{
$segmentValue = 'existing täs/ts';
$expectedSegmentFilter = 'city==existing+t%C3%A4s%2Fts';
$this->assertSegmentFilterForSegmentValueAndReport($this->report, $segmentValue, $expectedSegmentFilter);
}
public function test_filter_shouldNotOverwriteAnExistingSegmentValue()
{
$row = $this->addRowWithMetadata(array('segmentValue' => 'existing', 'segmentFilter' => 'city==mytest'));
$this->table->filter($this->filter, array($this->report));
$this->assertSegmentFilter('city==mytest', $row);
}
public function test_filter_shouldUseTheFirstSegment_IfAReportHasMultiple()
{
$report = new GetCountry();
$this->assertCount(2, $report->getDimension()->getSegments());
$this->assertSegmentFilterForSegmentValueAndReport($report, $segmentValue = 'existing', 'countryCode==existing');
}
public function test_filter_shouldNotGenerateASegmentFilter_IfReportHasNoDimension()
{
$report = new Get(); // VisitsSummary.get has no dimension
$this->assertNull($report->getDimension());
$this->assertSegmentFilterForSegmentValueAndReport($report, $segmentValue = 'existing', false);
}
public function test_filter_shouldNotGenerateASegmentFilter_IfDimensionHasNoSegmentFilter()
{
// outlinks currently has a dimensions but no segments, we have to use another report once it has segments
$report = new GetOutlinks();
$this->assertEmpty($report->getDimension()->getSegments());
$this->assertSegmentFilterForSegmentValueAndReport($report, $segmentValue = 'existing', false);
}
public function test_filter_shouldNotFail_IfNoReportGiven()
{
$this->assertSegmentFilterForSegmentValueAndReport($report = null, $segmentValue = 'existing', false);
}
public function test_filter_shouldNotFail_IfDataTableHasNoRows()
{
$table = new DataTable();
$table->filter($this->filter, array($this->report));
$this->assertSame(0, $table->getRowsCount());
}
private function assertSegmentFilterForSegmentValueAndReport($report, $segmentValue, $expectedSegmentFilter)
{
$row = $this->addRowWithMetadata(array('segmentValue' => $segmentValue));
$this->table->filter($this->filter, array($report));
$this->assertSegmentFilter($expectedSegmentFilter, $row);
}
private function assertSegmentFilter($expected, Row $row)
{
$segmentFilter = $row->getMetadata('segmentFilter');
$this->assertSame($expected, $segmentFilter);
}
}
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.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;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
/**
* @group AddSegmentFilterTest
* @group DataTable
* @group Filter
* @group Unit
* @group Core
*/
class AddSegmentFilterTest extends UnitTestCase
{
private $filter = 'AddSegmentFilter';
/**
* @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('segmentFilter');
$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('segmentFilter'));
}
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('segmentFilter');
$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('segmentFilter');
$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('segmentFilter');
$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('segmentFilter'));
}
}
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.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;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
/**
* @group AddSegmentValueTest
* @group DataTable
* @group Filter
* @group Unit
* @group Core
*/
class AddSegmentValueTest extends UnitTestCase
{
private $filter = 'AddSegmentValue';
/**
* @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 test'));
$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_shouldCopyTheLabelToMetadata_IfValueIsGiven()
{
$this->table->filter($this->filter);
$segmentValues = $this->table->getRowsMetadata('segmentValue');
$expected = array(
'http://piwik.org/test',
'',
'search+test',
'keyword test',
false,
'play A movie',
false,
'Piwik');
$this->assertSame($expected, $segmentValues);
}
public function test_filter_ShouldIgnoreSummaryRow()
{
$summaryRow = $this->buildRow(array('label' => 'my test'));
$this->table->addSummaryRow($summaryRow);
$this->table->filter($this->filter);
$this->assertFalse($summaryRow->getMetadata('segmentValue'));
}
public function test_filter_ShouldCallACallbackPassingTheLabel()
{
$this->table->filter($this->filter, array(function ($label) {
if ($label === false) {
return 'was false';
}
return 'piwik_' . $label;
}));
$segmentValues = $this->table->getRowsMetadata('segmentValue');
$expected = array(
'piwik_http://piwik.org/test',
'piwik_',
'piwik_search+test',
'piwik_keyword test',
'was false',
'piwik_play A movie',
'was false',
'piwik_Piwik');
$this->assertSame($expected, $segmentValues);
}
public function test_filter_shouldNotGenerateASegmentValueIfReturnValueIsFalse()
{
$this->table->filter($this->filter, array(function ($label) {
if ($label === false) {
return 'was false';
}
return false;
}));
$segmentValues = $this->table->getRowsMetadata('segmentValue');
$expected = array(
false,
false,
false,
false,
'was false',
false,
'was false',
false);
$this->assertSame($expected, $segmentValues);
}
}
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.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;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
/**
* @group ColumnCallbackDeleteMetadataTest
* @group DataTable
* @group Filter
* @group Unit
* @group Core
*/
class ColumnCallbackDeleteMetadataTest extends UnitTestCase
{
private $filter = 'ColumnCallbackDeleteMetadata';
/**
* @var DataTable
*/
private $table;
public function setUp()
{
$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 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_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);
}
}
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.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;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
/**
* @group PrependSegmentFilterTest
* @group DataTable
* @group Filter
* @group Unit
* @group Core
*/
class PrependSegmentFilterTest extends UnitTestCase
{
private $filter = 'PrependSegmentFilter';
/**
* @var DataTable
*/
private $table;
public function setUp()
{
$this->table = new DataTable();
$this->addRowWithMetadata(array('test' => '1'));
$this->addRowWithMetadata(array('test' => '2', 'segmentFilter' => 'country=NZ'));
$this->addRowWithMetadata(array('test' => '3'));
$this->addRowWithMetadata(array('test' => '1', 'segmentFilter' => 'country=AU'));
$this->addRowWithMetadata(array('test' => '4', 'segmentFilter' => ''));
}
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_shouldRemoveAllMetadataEntriesHavingTheGivenName()
{
$prepend = 'city=test;';
$this->table->filter($this->filter, array($prepend));
$metadata = $this->table->getRowsMetadata('segmentFilter');
$this->assertSame(array(
false,
$prepend . 'country=NZ',
false,
$prepend . 'country=AU',
$prepend),
$metadata);
// should be still the same
$metadata = $this->table->getRowsMetadata('test');
$this->assertSame(array('1', '2', '3', '1', '4'), $metadata);
}
}
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.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;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
/**
* @group PrependValueToMetadataTest
* @group DataTable
* @group Filter
* @group Unit
* @group Core
*/
class PrependValueToMetadataTest extends UnitTestCase
{
private $filter = 'PrependValueToMetadata';
/**
* @var DataTable
*/
private $table;
public function setUp()
{
$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);
}
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter