diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-08-11 18:29:58 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-08-11 18:29:58 +0400 |
commit | 1fa7995ced5189ecd4dc8787419d72bb591c2653 (patch) | |
tree | f92de23a844942a577f2bb5044ff775f8a3ee68d /tests | |
parent | c2a4f86902afd2e5bcdc179d86da718a8af7faba (diff) |
refs #5820 added some tests for reports and columns refactoring
Diffstat (limited to 'tests')
-rw-r--r-- | tests/PHPUnit/Core/Columns/DimensionTest.php | 145 | ||||
-rw-r--r-- | tests/PHPUnit/Core/Menu/MenuReportingTest.php | 84 | ||||
-rw-r--r-- | tests/PHPUnit/Core/Plugin/Dimension/ActionDimensionTest.php | 154 | ||||
-rw-r--r-- | tests/PHPUnit/Core/Plugin/Dimension/ConversionDimensionTest.php | 154 | ||||
-rw-r--r-- | tests/PHPUnit/Core/Plugin/Dimension/VisitDimensionTest.php | 254 | ||||
-rw-r--r-- | tests/PHPUnit/Core/Plugin/ReportTest.php | 429 |
6 files changed, 1214 insertions, 6 deletions
diff --git a/tests/PHPUnit/Core/Columns/DimensionTest.php b/tests/PHPUnit/Core/Columns/DimensionTest.php new file mode 100644 index 0000000000..47f0bfcbb1 --- /dev/null +++ b/tests/PHPUnit/Core/Columns/DimensionTest.php @@ -0,0 +1,145 @@ +<?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\Plugins\Test; +// there is a test that requires the class to be defined in a plugin + +use Piwik\Columns\Dimension; +use Piwik\Plugin\Segment; +use Piwik\Plugin\Manager; + +class DimensionTest extends Dimension +{ + protected $columnName = 'test_dimension'; + protected $columnType = 'INTEGER (10) DEFAULT 0'; + + public function set($param, $value) + { + $this->$param = $value; + } + + protected function configureSegments() + { + $segment = new Segment(); + $segment->setSegment('exitPageUrl'); + $segment->setName('Actions_ColumnExitPageURL'); + $segment->setCategory('General_Visit'); + $this->addSegment($segment); + + // custom type and sqlSegment + $segment = new Segment(); + $segment->setSegment('exitPageUrl'); + $segment->setSqlSegment('customValue'); + $segment->setType(Segment::TYPE_METRIC); + $segment->setName('Actions_ColumnExitPageURL'); + $segment->setCategory('General_Visit'); + $this->addSegment($segment); + } +} + +/** + * @group Core + */ +class Plugin_ActionDimensionTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var FakeActionDimension + */ + private $dimension; + + public function setUp() + { + Manager::getInstance()->unloadPlugins(); + + $this->dimension = new DimensionTest(); + } + + public function test_hasImplementedEvent_shouldDetectWhetherAMethodWasOverwrittenInTheActualPluginClass() + { + $this->assertTrue($this->dimension->hasImplementedEvent('set')); + $this->assertTrue($this->dimension->hasImplementedEvent('configureSegments')); + + $this->assertFalse($this->dimension->hasImplementedEvent('getSegments')); + } + + public function test_getColumnName_shouldReturnTheNameOfTheColumn() + { + $this->assertSame('test_dimension', $this->dimension->getColumnName()); + } + + public function test_hasColumnType_shouldDetectWhetherAColumnTypeIsSet() + { + $this->assertTrue($this->dimension->hasColumnType()); + + $this->dimension->set('columnType', ''); + $this->assertFalse($this->dimension->hasColumnType()); + } + + public function test_getName_ShouldNotReturnANameByDefault() + { + $this->assertSame('', $this->dimension->getName()); + } + + public function test_getAllDimensions_shouldReturnActionVisitAndConversionDimensions() + { + Manager::getInstance()->loadPlugin('Actions'); + Manager::getInstance()->loadPlugin('Events'); + Manager::getInstance()->loadPlugin('DevicesDetector'); + Manager::getInstance()->loadPlugin('Goals'); + + $dimensions = Dimension::getAllDimensions(); + + $this->assertGreaterThan(20, count($dimensions)); + + $foundConversion = false; + $foundVisit = false; + $foundAction = false; + + foreach ($dimensions as $dimension) { + if ($dimension instanceof \Piwik\Plugin\Dimension\ConversionDimension) { + $foundConversion = true; + } else if ($dimension instanceof \Piwik\Plugin\Dimension\ActionDimension) { + $foundAction = true; + } else if ($dimension instanceof \Piwik\Plugin\Dimension\VisitDimension) { + $foundVisit = true; + } else { + $this->fail('Unexpected dimension class found'); + } + + $this->assertRegExp('/Piwik.Plugins.(Actions|Events|DevicesDetector|Goals).Columns/', get_class($dimension)); + } + + $this->assertTrue($foundConversion); + $this->assertTrue($foundAction); + $this->assertTrue($foundVisit); + } + + public function test_getSegment_ShouldReturnConfiguredSegments() + { + $segments = $this->dimension->getSegments(); + + $this->assertCount(2, $segments); + $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[0]); + $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[1]); + } + + public function test_addSegment_ShouldPrefilSomeSegmentValuesIfNotDefinedYet() + { + $segments = $this->dimension->getSegments(); + + $this->assertEquals(Segment::TYPE_DIMENSION, $segments[0]->getType()); + } + + public function test_addSegment_ShouldNotOverwritePreAssignedValues() + { + $segments = $this->dimension->getSegments(); + + $this->assertEquals(Segment::TYPE_METRIC, $segments[1]->getType()); + } + +}
\ No newline at end of file diff --git a/tests/PHPUnit/Core/Menu/MenuReportingTest.php b/tests/PHPUnit/Core/Menu/MenuReportingTest.php new file mode 100644 index 0000000000..64be977b46 --- /dev/null +++ b/tests/PHPUnit/Core/Menu/MenuReportingTest.php @@ -0,0 +1,84 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +use Piwik\Plugin\Report; +use Piwik\Plugins\ExampleReport\Reports\GetExampleReport; +use Piwik\Plugins\Actions\Columns\ExitPageUrl; +use Piwik\Piwik; +use Piwik\Metrics; +use Piwik\WidgetsList; +use Piwik\Translate; +use Piwik\Menu\MenuReporting; +use Piwik\Plugin\Manager as PluginManager; + + +/** + * @group Core + */ +class Plugin_ReportTest extends PHPUnit_Framework_TestCase +{ + /** + * @var MenuReporting + */ + private $menu; + + public function setUp() + { + PluginManager::getInstance()->unloadPlugins(); + $this->menu = MenuReporting::getInstance(); + } + + public function tearDown() + { + MenuReporting::getInstance()->unsetInstance(); + parent::tearDown(); + } + + public function test_getMenu_shouldBeNull_IfNoItems() + { + $this->assertNull($this->menu->getMenu()); + } + + public function test_getMenu_shouldTriggerAddItemsEvent_toBeBackwardsCompatible() + { + $this->loadSomePlugins(); + + $triggered = false; + Piwik::addAction('Menu.Reporting.addItems', function () use (&$triggered) { + $triggered = true; + }); + + $this->menu->getMenu(); + + $this->assertTrue($triggered); + } + + public function test_getMenu_shouldAddMenuItemsOfReports() + { + $this->loadSomePlugins(); + + $items = $this->menu->getMenu(); + + $this->assertNotEmpty($items); + $this->assertGreaterThan(20, $items); + $this->assertEquals(array('General_Actions', 'General_Visitors'), array_keys($items)); + $this->assertNotEmpty($items['General_Actions']['General_Pages']); + $this->assertEquals('menuGetPageUrls', $items['General_Actions']['General_Pages']['_url']['action']); + } + + private function loadSomePlugins() + { + PluginManager::getInstance()->loadPlugin('Actions'); + PluginManager::getInstance()->loadPlugin('DevicesDetection'); + PluginManager::getInstance()->loadPlugin('CoreVisualizations'); + PluginManager::getInstance()->loadPlugin('API'); + PluginManager::getInstance()->loadPlugin('Morpheus'); + } + + +}
\ No newline at end of file diff --git a/tests/PHPUnit/Core/Plugin/Dimension/ActionDimensionTest.php b/tests/PHPUnit/Core/Plugin/Dimension/ActionDimensionTest.php new file mode 100644 index 0000000000..fe3071d56c --- /dev/null +++ b/tests/PHPUnit/Core/Plugin/Dimension/ActionDimensionTest.php @@ -0,0 +1,154 @@ +<?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\Plugins\Test; +// there is a test that requires the class to be defined in a plugin + +use Piwik\Plugin\Dimension\ActionDimension; +use Piwik\Plugin\Segment; +use Piwik\Plugin\Manager; + +class FakeActionDimension extends ActionDimension +{ + protected $columnName = 'fake_action_dimension_column'; + protected $columnType = 'INTEGER (10) DEFAULT 0'; + + public function set($param, $value) + { + $this->$param = $value; + } + + protected function configureSegments() + { + $segment = new Segment(); + $segment->setSegment('exitPageUrl'); + $segment->setName('Actions_ColumnExitPageURL'); + $segment->setCategory('General_Visit'); + $this->addSegment($segment); + + // custom type and sqlSegment + $segment = new Segment(); + $segment->setSegment('exitPageUrl'); + $segment->setSqlSegment('customValue'); + $segment->setType(Segment::TYPE_METRIC); + $segment->setName('Actions_ColumnExitPageURL'); + $segment->setCategory('General_Visit'); + $this->addSegment($segment); + } +} + +/** + * @group Core + */ +class Plugin_ActionDimensionTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var FakeActionDimension + */ + private $dimension; + + public function setUp() + { + Manager::getInstance()->unloadPlugins(); + + $this->dimension = new FakeActionDimension(); + } + + public function test_install_shouldNotReturnAnything_IfColumnTypeNotSpecified() + { + $this->dimension->set('columnType', ''); + $this->assertEquals(array(), $this->dimension->install()); + } + + public function test_install_shouldNotReturnAnything_IfColumnNameNotSpecified() + { + $this->dimension->set('columnName', ''); + $this->assertEquals(array(), $this->dimension->install()); + } + + public function test_install_shouldAlwaysInstallLogAction_IfColumnNameAndTypeGiven() + { + $expected = array( + 'log_link_visit_action' => array( + "ADD COLUMN `fake_action_dimension_column` INTEGER (10) DEFAULT 0" + ) + ); + + $this->assertEquals($expected, $this->dimension->install()); + } + + public function test_update_shouldAlwaysUpdateLogVisit_IfColumnNameAndTypeGiven() + { + $expected = array( + 'log_link_visit_action' => array( + "MODIFY COLUMN `fake_action_dimension_column` INTEGER (10) DEFAULT 0" + ) + ); + + $this->assertEquals($expected, $this->dimension->update(array())); + } + + public function test_getVersion_shouldUseColumnTypeAsVersion() + { + $this->assertEquals('INTEGER (10) DEFAULT 0', $this->dimension->getVersion()); + } + + public function test_getSegment_ShouldReturnConfiguredSegments() + { + $segments = $this->dimension->getSegments(); + + $this->assertCount(2, $segments); + $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[0]); + $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[1]); + } + + public function test_addSegment_ShouldPrefilSomeSegmentValuesIfNotDefinedYet() + { + $segments = $this->dimension->getSegments(); + + $this->assertEquals('log_link_visit_action.fake_action_dimension_column', $segments[0]->getSqlSegment()); + $this->assertEquals(Segment::TYPE_DIMENSION, $segments[0]->getType()); + } + + public function test_addSegment_ShouldNotOverwritePreAssignedValues() + { + $segments = $this->dimension->getSegments(); + + $this->assertEquals('customValue', $segments[1]->getSqlSegment()); + $this->assertEquals(Segment::TYPE_METRIC, $segments[1]->getType()); + } + + public function test_getDimensions_shouldOnlyLoadAllActionDimensionsFromACertainPlugin() + { + $plugin = Manager::getInstance()->loadPlugin('Actions'); + + $dimensions = ActionDimension::getDimensions($plugin); + + $this->assertGreaterThan(5, count($dimensions)); + + foreach ($dimensions as $dimension) { + $this->assertInstanceOf('\Piwik\Plugin\Dimension\ActionDimension', $dimension); + $this->assertStringStartsWith('Piwik\Plugins\Actions\Columns', get_class($dimension)); + } + } + + public function test_getAllDimensions_shouldLoadAllDimensionsButOnlyIfLoadedPlugins() + { + Manager::getInstance()->loadPlugin('Actions'); + Manager::getInstance()->loadPlugin('Events'); + + $dimensions = ActionDimension::getAllDimensions(); + + $this->assertGreaterThan(9, count($dimensions)); + + foreach ($dimensions as $dimension) { + $this->assertInstanceOf('\Piwik\Plugin\Dimension\ActionDimension', $dimension); + $this->assertRegExp('/Piwik.Plugins.(Actions|Events).Columns/', get_class($dimension)); + } + } +}
\ No newline at end of file diff --git a/tests/PHPUnit/Core/Plugin/Dimension/ConversionDimensionTest.php b/tests/PHPUnit/Core/Plugin/Dimension/ConversionDimensionTest.php new file mode 100644 index 0000000000..37988dee2a --- /dev/null +++ b/tests/PHPUnit/Core/Plugin/Dimension/ConversionDimensionTest.php @@ -0,0 +1,154 @@ +<?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\Plugins\Test; +// there is a test that requires the class to be defined in a plugin + +use Piwik\Plugin\Dimension\ConversionDimension; +use Piwik\Plugin\Segment; +use Piwik\Plugin\Manager; + +class FakeConversionDimension extends ConversionDimension +{ + protected $columnName = 'fake_conversion_dimension_column'; + protected $columnType = 'INTEGER (10) DEFAULT 0'; + + public function set($param, $value) + { + $this->$param = $value; + } + + protected function configureSegments() + { + $segment = new Segment(); + $segment->setSegment('exitPageUrl'); + $segment->setName('Actions_ColumnExitPageURL'); + $segment->setCategory('General_Visit'); + $this->addSegment($segment); + + // custom type and sqlSegment + $segment = new Segment(); + $segment->setSegment('exitPageUrl'); + $segment->setSqlSegment('customValue'); + $segment->setType(Segment::TYPE_METRIC); + $segment->setName('Actions_ColumnExitPageURL'); + $segment->setCategory('General_Visit'); + $this->addSegment($segment); + } +} + +/** + * @group Core + */ +class Plugin_ConversionDimensionTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var FakeConversionDimension + */ + private $dimension; + + public function setUp() + { + Manager::getInstance()->unloadPlugins(); + + $this->dimension = new FakeConversionDimension(); + } + + public function test_install_shouldNotReturnAnything_IfColumnTypeNotSpecified() + { + $this->dimension->set('columnType', ''); + $this->assertEquals(array(), $this->dimension->install()); + } + + public function test_install_shouldNotReturnAnything_IfColumnNameNotSpecified() + { + $this->dimension->set('columnName', ''); + $this->assertEquals(array(), $this->dimension->install()); + } + + public function test_install_shouldAlwaysInstallLogAction_IfColumnNameAndTypeGiven() + { + $expected = array( + 'log_conversion' => array( + "ADD COLUMN `fake_conversion_dimension_column` INTEGER (10) DEFAULT 0" + ) + ); + + $this->assertEquals($expected, $this->dimension->install()); + } + + public function test_update_shouldAlwaysUpdateLogVisit_IfColumnNameAndTypeGiven() + { + $expected = array( + 'log_conversion' => array( + "MODIFY COLUMN `fake_conversion_dimension_column` INTEGER (10) DEFAULT 0" + ) + ); + + $this->assertEquals($expected, $this->dimension->update(array())); + } + + public function test_getVersion_shouldUseColumnTypeAsVersion() + { + $this->assertEquals('INTEGER (10) DEFAULT 0', $this->dimension->getVersion()); + } + + public function test_getSegment_ShouldReturnConfiguredSegments() + { + $segments = $this->dimension->getSegments(); + + $this->assertCount(2, $segments); + $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[0]); + $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[1]); + } + + public function test_addSegment_ShouldPrefilSomeSegmentValuesIfNotDefinedYet() + { + $segments = $this->dimension->getSegments(); + + $this->assertEquals('log_conversion.fake_conversion_dimension_column', $segments[0]->getSqlSegment()); + $this->assertEquals(Segment::TYPE_DIMENSION, $segments[0]->getType()); + } + + public function test_addSegment_ShouldNotOverwritePreAssignedValues() + { + $segments = $this->dimension->getSegments(); + + $this->assertEquals('customValue', $segments[1]->getSqlSegment()); + $this->assertEquals(Segment::TYPE_METRIC, $segments[1]->getType()); + } + + public function test_getDimensions_shouldOnlyLoadAllConversionDimensionsFromACertainPlugin() + { + $plugin = Manager::getInstance()->loadPlugin('ExampleTracker'); + + $dimensions = ConversionDimension::getDimensions($plugin); + + $this->assertGreaterThanOrEqual(1, count($dimensions)); + + foreach ($dimensions as $dimension) { + $this->assertInstanceOf('\Piwik\Plugin\Dimension\ConversionDimension', $dimension); + $this->assertStringStartsWith('Piwik\Plugins\ExampleTracker\Columns', get_class($dimension)); + } + } + + public function test_getAllDimensions_shouldLoadAllDimensionsButOnlyIfLoadedPlugins() + { + Manager::getInstance()->loadPlugin('ExampleTracker'); + Manager::getInstance()->loadPlugin('Goals'); + + $dimensions = ConversionDimension::getAllDimensions(); + + $this->assertGreaterThan(5, count($dimensions)); + + foreach ($dimensions as $dimension) { + $this->assertInstanceOf('\Piwik\Plugin\Dimension\ConversionDimension', $dimension); + $this->assertRegExp('/Piwik.Plugins.(ExampleTracker|Goals).Columns/', get_class($dimension)); + } + } +}
\ No newline at end of file diff --git a/tests/PHPUnit/Core/Plugin/Dimension/VisitDimensionTest.php b/tests/PHPUnit/Core/Plugin/Dimension/VisitDimensionTest.php new file mode 100644 index 0000000000..3cc61c5da4 --- /dev/null +++ b/tests/PHPUnit/Core/Plugin/Dimension/VisitDimensionTest.php @@ -0,0 +1,254 @@ +<?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\Plugins\Test; +// there is a test that requires the class to be defined in a plugin + +use Piwik\Plugin\Dimension\VisitDimension; +use Piwik\Plugin\Segment; +use Piwik\Plugin\Manager; +use Piwik\Tracker\Request; +use Piwik\Tracker\Visitor; + +class FakeVisitDimension extends VisitDimension +{ + protected $columnName = 'fake_visit_dimension_column'; + protected $columnType = 'INTEGER (10) DEFAULT 0'; + public $requiredFields = array(); + + public function set($param, $value) + { + $this->$param = $value; + } + + public function getRequiredVisitFields() + { + return $this->requiredFields; + } +} + +class FakeConversionVisitDimension extends FakeVisitDimension +{ + public function onAnyGoalConversion(Request $request, Visitor $visitor, $action) + { + return false; + } + + protected function configureSegments() + { + $segment = new Segment(); + $segment->setSegment('exitPageUrl'); + $segment->setName('Actions_ColumnExitPageURL'); + $segment->setCategory('General_Visit'); + $this->addSegment($segment); + + // custom type and sqlSegment + $segment = new Segment(); + $segment->setSegment('exitPageUrl'); + $segment->setSqlSegment('customValue'); + $segment->setType(Segment::TYPE_METRIC); + $segment->setName('Actions_ColumnExitPageURL'); + $segment->setCategory('General_Visit'); + $this->addSegment($segment); + } +} + +/** + * @group Core + */ +class Plugin_VisitDimensionTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var FakeVisitDimension + */ + private $dimension; + + /** + * @var FakeConversionVisitDimension + */ + private $conversionDimension; + + public function setUp() + { + Manager::getInstance()->unloadPlugins(); + + $this->dimension = new FakeVisitDimension(); + $this->conversionDimension = new FakeConversionVisitDimension(); + } + + public function test_install_shouldNotReturnAnything_IfColumnTypeNotSpecified() + { + $this->dimension->set('columnType', ''); + $this->assertEquals(array(), $this->dimension->install()); + } + + public function test_install_shouldNotReturnAnything_IfColumnNameNotSpecified() + { + $this->dimension->set('columnName', ''); + $this->assertEquals(array(), $this->dimension->install()); + } + + public function test_install_shouldAlwaysInstallLogVisit_IfColumnNameAndTypeGiven() + { + $expected = array( + 'log_visit' => array( + "ADD COLUMN `fake_visit_dimension_column` INTEGER (10) DEFAULT 0" + ) + ); + + $this->assertEquals($expected, $this->dimension->install()); + } + + public function test_install_shouldInstallLogVisitAndConversion_IfConversionMethodIsImplemented() + { + $expected = array( + 'log_visit' => array( + "ADD COLUMN `fake_visit_dimension_column` INTEGER (10) DEFAULT 0" + ), + 'log_conversion' => array( + "ADD COLUMN `fake_visit_dimension_column` INTEGER (10) DEFAULT 0" + ) + ); + + $this->assertEquals($expected, $this->conversionDimension->install()); + } + + public function test_update_shouldAlwaysUpdateLogVisit_IfColumnNameAndTypeGiven() + { + $expected = array( + 'log_visit' => array( + "MODIFY COLUMN `fake_visit_dimension_column` INTEGER (10) DEFAULT 0" + ) + ); + + $this->assertEquals($expected, $this->dimension->update(array())); + } + + public function test_update_shouldUpdateLogVisitAndAddConversion_IfConversionMethodIsImplementedButNotInstalledYet() + { + $expected = array( + 'log_visit' => array( + "MODIFY COLUMN `fake_visit_dimension_column` INTEGER (10) DEFAULT 0" + ), + 'log_conversion' => array( + "ADD COLUMN `fake_visit_dimension_column` INTEGER (10) DEFAULT 0" + ) + ); + + $this->assertEquals($expected, $this->conversionDimension->update(array())); + } + + public function test_update_shouldUpdateLogVisitAndConversion_IfConversionMethodIsImplementedAndInstalled() + { + $expected = array( + 'log_visit' => array( + "MODIFY COLUMN `fake_visit_dimension_column` INTEGER (10) DEFAULT 0" + ), + 'log_conversion' => array( + "MODIFY COLUMN `fake_visit_dimension_column` INTEGER (10) DEFAULT 0" + ) + ); + + $this->assertEquals($expected, $this->conversionDimension->update(array('fake_visit_dimension_column' => array()))); + } + + public function test_getVersion_shouldUseColumnTypeAsVersion() + { + $this->assertEquals('INTEGER (10) DEFAULT 0', $this->dimension->getVersion()); + } + + public function test_getVersion_shouldIncludeConversionMethodIntoVersionNumber_ToMakeSureUpdateMethodWillBeTriggeredWhenPluginAddedConversionMethodInNewVersion() + { + $this->assertEquals('INTEGER (10) DEFAULT 01', $this->conversionDimension->getVersion()); + } + + public function test_getSegment_ShouldReturnNoSegments_IfNoneConfigured() + { + $this->assertEquals(array(), $this->dimension->getSegments()); + } + + public function test_getSegment_ShouldReturnConfiguredSegments() + { + $segments = $this->conversionDimension->getSegments(); + + $this->assertCount(2, $segments); + $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[0]); + $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[1]); + } + + public function test_addSegment_ShouldPrefilSomeSegmentValuesIfNotDefinedYet() + { + $segments = $this->conversionDimension->getSegments(); + + $this->assertEquals('log_visit.fake_visit_dimension_column', $segments[0]->getSqlSegment()); + $this->assertEquals(Segment::TYPE_DIMENSION, $segments[0]->getType()); + } + + public function test_addSegment_ShouldNotOverwritePreAssignedValues() + { + $segments = $this->conversionDimension->getSegments(); + + $this->assertEquals('customValue', $segments[1]->getSqlSegment()); + $this->assertEquals(Segment::TYPE_METRIC, $segments[1]->getType()); + } + + public function test_sortByRequiredFields_ShouldResolveDependencies() + { + $dimension1 = new FakeVisitDimension(); + $dimension1->set('columnName', 'column1'); + $dimension1->requiredFields = array('column2'); + + $dimension2 = new FakeVisitDimension(); + $dimension2->set('columnName', 'column2'); + $dimension2->requiredFields = array('column3', 'column4'); + + $dimension3 = new FakeVisitDimension(); + $dimension3->set('columnName', 'column3'); + $dimension3->requiredFields = array(); + + $dimension4 = new FakeVisitDimension(); + $dimension4->set('columnName', 'column4'); + $dimension4->requiredFields = array('column3'); + + $instances = array($dimension1, $dimension2, $dimension3, $dimension4); + + usort($instances, array('\Piwik\Plugin\Dimension\VisitDimension', 'sortByRequiredFields')); + + $this->assertSame(array($dimension3, $dimension4, $dimension2, $dimension1), $instances); + } + + public function test_getDimensions_shouldOnlyLoadAllVisitDimensionsFromACertainPlugin() + { + $plugin = Manager::getInstance()->loadPlugin('Actions'); + + $dimensions = VisitDimension::getDimensions($plugin); + + $this->assertGreaterThan(5, count($dimensions)); + + foreach ($dimensions as $dimension) { + $this->assertInstanceOf('\Piwik\Plugin\Dimension\VisitDimension', $dimension); + $this->assertStringStartsWith('Piwik\Plugins\Actions\Columns', get_class($dimension)); + } + } + + + public function test_getAllDimensions_shouldLoadAllDimensionsButOnlyIfLoadedPlugins() + { + Manager::getInstance()->loadPlugin('Actions'); + Manager::getInstance()->loadPlugin('DevicesDetection'); + + $dimensions = VisitDimension::getAllDimensions(); + + $this->assertGreaterThan(10, count($dimensions)); + + foreach ($dimensions as $dimension) { + $this->assertInstanceOf('\Piwik\Plugin\Dimension\VisitDimension', $dimension); + $this->assertRegExp('/Piwik.Plugins.(DevicesDetection|Actions).Columns/', get_class($dimension)); + } + } +}
\ No newline at end of file diff --git a/tests/PHPUnit/Core/Plugin/ReportTest.php b/tests/PHPUnit/Core/Plugin/ReportTest.php index e5b1edce1c..7c28b39218 100644 --- a/tests/PHPUnit/Core/Plugin/ReportTest.php +++ b/tests/PHPUnit/Core/Plugin/ReportTest.php @@ -8,7 +8,60 @@ use Piwik\Plugin\Report; use Piwik\Plugins\ExampleReport\Reports\GetExampleReport; +use Piwik\Plugins\Actions\Columns\ExitPageUrl; +use Piwik\Piwik; use Piwik\Metrics; +use Piwik\WidgetsList; +use Piwik\Translate; +use Piwik\Menu\MenuReporting; +use Piwik\Plugin\Manager as PluginManager; + +class GetBasicReport extends Report +{ + protected function init() + { + parent::init(); + + $this->name = 'My Custom Report Name'; + $this->order = 20; + $this->module = 'TestPlugin'; + $this->action = 'getBasicReport'; + $this->category = 'Goals_Goals'; + } +} + +class GetAdvancedReport extends GetBasicReport +{ + protected function init() + { + parent::init(); + + $this->action = 'getAdvancedReport'; + $this->widgetTitle = 'Actions_WidgetPageTitlesFollowingSearch'; + $this->menuTitle = 'Actions_SubmenuPageTitles'; + $this->documentation = Piwik::translate('ExampleReportDocumentation'); + $this->dimension = new ExitPageUrl(); + $this->metrics = array('nb_actions', 'nb_visits'); + $this->processedMetrics = array('conversion_rate', 'bounce_rate'); + $this->parameters = array('idGoal' => 1); + $this->isSubtableReport = true; + $this->actionToLoadSubTables = 'GetBasicReport'; + $this->constantRowsCount = true; + } + + public function set($param, $value) + { + $this->$param = $value; + } +} + +class GetDisabledReport extends GetBasicReport +{ + public function isEnabled() + { + return false; + } +} /** * @group Core @@ -18,20 +71,384 @@ class Plugin_ReportTest extends PHPUnit_Framework_TestCase /** * @var Report */ - private $report; + private $exampleReport; + + /** + * @var GetDisabledReport + */ + private $disabledReport; + + /** + * @var GetBasicReport + */ + private $basicReport; + + /** + * @var GetAdvancedReport + */ + private $advancedReport; public function setUp() { - $this->report = new GetExampleReport(); + $this->exampleReport = new GetExampleReport(); + $this->disabledReport = new GetDisabledReport(); + $this->basicReport = new GetBasicReport(); + $this->advancedReport = new GetAdvancedReport(); } - public function test_reportShouldUseDefaultMetrics() + public function tearDown() { - $this->assertEquals(Metrics::getDefaultMetrics(), $this->report->getMetrics()); + WidgetsList::getInstance()->_reset(); + MenuReporting::getInstance()->unsetInstance(); + Translate::unloadEnglishTranslation(); + parent::tearDown(); + } + + public function test_shouldDetectTheModuleOfTheReportAutomatically() + { + $this->assertEquals('ExampleReport', $this->exampleReport->getModule()); + } + + public function test_shouldDetectTheActionOfTheReportAutomatiacally() + { + $this->assertEquals('getExampleReport', $this->exampleReport->getAction()); + } + + public function test_getName_shouldReturnTheNameOfTheReport() + { + $this->assertEquals('My Custom Report Name', $this->basicReport->getName()); + } + + public function test_isEnabled_shouldBeEnabledByDefault() + { + $this->assertTrue($this->basicReport->isEnabled()); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage General_ExceptionReportNotEnabled + */ + public function test_checkIsEnabled_shouldThrowAnExceptionIfReportIsNotEnabled() + { + $this->disabledReport->checkIsEnabled(); + } + + public function test_getWidgetTitle_shouldReturnNullIfNoTitleIsSet() + { + $this->assertNull($this->basicReport->getWidgetTitle()); + } + + public function test_getWidgetTitle_shouldReturnTranslatedTitleIfSet() + { + Translate::loadEnglishTranslation(); + $this->assertEquals('Page Titles Following a Site Search', $this->advancedReport->getWidgetTitle()); + } + + public function test_getCategory_shouldReturnTranslatedCategory() + { + Translate::loadEnglishTranslation(); + $this->assertEquals('Goals', $this->advancedReport->getCategory()); + } + + public function test_configureWidget_shouldNotAddAWidgetIfNoWidgetTitleIsSet() + { + $widgets = WidgetsList::get(); + $this->assertCount(0, $widgets); + + $this->basicReport->configureWidget(WidgetsList::getInstance()); + + $widgets = WidgetsList::get(); + $this->assertCount(0, $widgets); + } + + public function test_configureWidget_shouldAddAWidgetIfAWidgetTitleIsSet() + { + $widgets = WidgetsList::get(); + $this->assertCount(0, $widgets); + + $this->advancedReport->configureWidget(WidgetsList::getInstance()); + + $widgets = WidgetsList::get(); + $this->assertCount(1, $widgets); + $this->assertEquals(array(array( + 'name' => 'Actions_WidgetPageTitlesFollowingSearch', + 'uniqueId' => 'widgetTestPlugingetAdvancedReport', + 'parameters' => array('module' => 'TestPlugin', 'action' => 'getAdvancedReport') + )), $widgets['Goals_Goals']); + } + + public function test_configureWidget_shouldMixinWidgetParametersIfSet() + { + $widgets = WidgetsList::get(); + $this->assertCount(0, $widgets); + + $this->advancedReport->set('widgetParams', array('foo' => 'bar')); + $this->advancedReport->configureWidget(WidgetsList::getInstance()); + + $widgets = WidgetsList::get(); + $this->assertCount(1, $widgets); + $this->assertEquals(array('module' => 'TestPlugin', 'action' => 'getAdvancedReport', 'foo' => 'bar'), + $widgets['Goals_Goals'][0]['parameters']); + } + + public function test_configureReportingMenu_shouldNotAddAMenuIfNoWidgetTitleIsSet() + { + $menu = MenuReporting::getInstance(); + $menuItems = $menu->getMenu(); + $this->assertNull($menuItems); + + $this->basicReport->configureReportingMenu($menu); + + $menuItems = $menu->getMenu(); + $this->assertNull($menuItems); + } + + public function test_configureReportingMenu_shouldAddAMenuIfATitleIsSet() + { + $menu = MenuReporting::getInstance(); + $menuItems = $menu->getMenu(); + $this->assertNull($menuItems); + + $this->advancedReport->configureReportingMenu($menu); + + $menuItems = $menu->getMenu(); + + $expected = array( + '_tooltip' => '', + '_order' => 20, + '_hasSubmenu' => 1, + 'Actions_SubmenuPageTitles' => array( + '_url' => array( + 'module' => 'TestPlugin', + 'action' => 'menuGetAdvancedReport', + 'idSite' => '', + ), + '_order' => 20, + '_name' => 'Actions_SubmenuPageTitles', + '_tooltip' => '', + )); + + $this->assertCount(1, $menuItems); + $this->assertEquals($expected, $menuItems['Goals_Goals']); } - public function test_reportShouldUseDefaultProcessedMetrics() + public function test_getMetrics_shouldUseDefaultMetrics() { - $this->assertEquals(Metrics::getDefaultProcessedMetrics(), $this->report->getProcessedMetrics()); + $this->assertEquals(Metrics::getDefaultMetrics(), $this->basicReport->getMetrics()); } + + public function test_getMetrics_shouldReturnEmptyArray_IfNoMetricsDefined() + { + $this->advancedReport->set('metrics', array()); + $this->assertEquals(array(), $this->advancedReport->getMetrics()); + } + + public function test_getMetrics_shouldFindTranslationsForMetricsAndReturnOnlyTheOnesDefinedInSameOrder() + { + $expected = array( + 'nb_visits' => 'General_ColumnNbVisits', + 'nb_actions' => 'General_ColumnNbActions' + ); + $this->assertEquals($expected, $this->advancedReport->getMetrics()); + } + + public function test_getProcessedMetrics_shouldReturnConfiguredValue_IfNotAnArrayGivenToPreventDefaultMetrics() + { + $this->advancedReport->set('processedMetrics', false); + $this->assertEquals(false, $this->advancedReport->getProcessedMetrics()); + } + + public function test_getProcessedMetrics_shouldReturnEmptyArray_IfNoMetricsDefined() + { + $this->advancedReport->set('processedMetrics', array()); + $this->assertEquals(array(), $this->advancedReport->getProcessedMetrics()); + } + + public function test_getProcessedMetrics_reportShouldUseDefaultProcessedMetrics() + { + $this->assertEquals(Metrics::getDefaultProcessedMetrics(), $this->basicReport->getProcessedMetrics()); + } + + public function test_getProcessedMetrics_shouldFindTranslationsForMetricsAndReturnOnlyTheOnesDefinedInSameOrder() + { + $expected = array( + 'conversion_rate' => 'General_ColumnConversionRate', + 'bounce_rate' => 'General_ColumnBounceRate' + ); + $this->assertEquals($expected, $this->advancedReport->getProcessedMetrics()); + } + + public function test_hasGoalMetrics_shouldBeDisabledByDefault() + { + $this->assertFalse($this->advancedReport->hasGoalMetrics()); + } + + public function test_hasGoalMetrics_shouldReturnGoalMetricsProperty() + { + $this->advancedReport->set('hasGoalMetrics', true); + $this->assertTrue($this->advancedReport->hasGoalMetrics()); + } + + public function test_configureReportMetadata_shouldNotAddAReportIfReportIsDisabled() + { + $reports = array(); + $this->disabledReport->configureReportMetadata($reports, array()); + $this->assertEquals(array(), $reports); + } + + public function test_configureReportMetadata_shouldAddAReportIfReportIsEnabled() + { + $reports = array(); + $this->basicReport->configureReportMetadata($reports, array()); + $this->assertCount(1, $reports); + } + + public function test_configureReportMetadata_shouldBuiltStructureAndIncludeOnlyFieldsThatAreSet() + { + $reports = array(); + $this->basicReport->configureReportMetadata($reports, array()); + $this->assertEquals(array( + array( + 'category' => 'Goals_Goals', + 'name' => 'My Custom Report Name', + 'module' => 'TestPlugin', + 'action' => 'getBasicReport', + 'metrics' => array( + 'nb_visits' => 'General_ColumnNbVisits', + 'nb_uniq_visitors' => 'General_ColumnNbUniqVisitors', + 'nb_actions' => 'General_ColumnNbActions', + ), + 'metricsDocumentation' => array( + 'nb_visits' => 'General_ColumnNbVisitsDocumentation', + 'nb_uniq_visitors' => 'General_ColumnNbUniqVisitorsDocumentation', + 'nb_actions' => 'General_ColumnNbActionsDocumentation', + ), + 'processedMetrics' => array( + 'nb_actions_per_visit' => 'General_ColumnActionsPerVisit', + 'avg_time_on_site' => 'General_ColumnAvgTimeOnSite', + 'bounce_rate' => 'General_ColumnBounceRate', + 'conversion_rate' => 'General_ColumnConversionRate', + ), + 'order' => '20' + ) + ), $reports); + } + + public function test_configureReportMetadata_shouldBuiltStructureAllFieldsSet() + { + $reports = array(); + $this->advancedReport->configureReportMetadata($reports, array()); + $this->assertEquals(array( + array( + 'category' => 'Goals_Goals', + 'name' => 'My Custom Report Name', + 'module' => 'TestPlugin', + 'action' => 'getAdvancedReport', + 'parameters' => array( + 'idGoal' => 1 + ), + 'dimension' => 'Actions_ColumnExitPageURL', + 'documentation' => 'ExampleReportDocumentation', + 'isSubtableReport' => true, + 'metrics' => array( + 'nb_actions' => 'General_ColumnNbActions', + 'nb_visits' => 'General_ColumnNbVisits' + ), + 'metricsDocumentation' => array( + 'nb_actions' => 'General_ColumnNbActionsDocumentation', + 'nb_visits' => 'General_ColumnNbVisitsDocumentation', + ), + 'processedMetrics' => array( + 'conversion_rate' => 'General_ColumnConversionRate', + 'bounce_rate' => 'General_ColumnBounceRate', + ), + 'actionToLoadSubTables' => 'GetBasicReport', + 'constantRowsCount' => true, + 'order' => '20' + ) + ), $reports); + } + + public function test_factory_shouldNotFindAReportIfReportExistsButPluginIsNotLoaded() + { + $this->unloadAllPlugins(); + + $report = Report::factory('ExampleReport', 'getExampleReport'); + + $this->assertNull($report); + } + + public function test_factory_shouldFindAReportThatExists() + { + $this->loadExampleReportPlugin(); + + $module = 'ExampleReport'; + $action = 'getExampleReport'; + + $report = Report::factory($module, $action); + + $this->assertInstanceOf('Piwik\Plugins\ExampleReport\Reports\GetExampleReport', $report); + $this->assertEquals($module, $report->getModule()); + $this->assertEquals($action, $report->getAction()); + + // action ucfirst should work as well + $report = Report::factory($module, ucfirst($action)); + $this->assertInstanceOf('Piwik\Plugins\ExampleReport\Reports\GetExampleReport', $report); + } + + public function test_factory_shouldNotFindAReportIfPluginLoadedButReportNotExists() + { + $this->loadExampleReportPlugin(); + + $module = 'ExampleReport'; + $action = 'NotExistingReport'; + + $report = Report::factory($module, $action); + + $this->assertNull($report); + } + + public function test_getAllReports_shouldNotFindAReportIfNoPluginLoaded() + { + $this->unloadAllPlugins(); + + $report = Report::getAllReports(); + + $this->assertEquals(array(), $report); + } + + public function test_getAllReports_shouldFindAReportIfPluginLoadedButReportNotExists() + { + $this->loadExampleReportPlugin(); + $this->loadMorePlugins(); + + $reports = Report::getAllReports(); + + $this->assertGreaterThan(20, count($reports)); + + foreach ($reports as $report) { + $this->assertInstanceOf('Piwik\Plugin\Report', $report); + } + } + + private function loadExampleReportPlugin() + { + PluginManager::getInstance()->loadPlugin('ExampleReport'); + } + + private function loadMorePlugins() + { + PluginManager::getInstance()->loadPlugin('Actions'); + PluginManager::getInstance()->loadPlugin('DevicesDetection'); + PluginManager::getInstance()->loadPlugin('CoreVisualizations'); + PluginManager::getInstance()->loadPlugin('API'); + PluginManager::getInstance()->loadPlugin('Morpheus'); + } + + private function unloadAllPlugins() + { + PluginManager::getInstance()->unloadPlugins(); + } + + }
\ No newline at end of file |