diff options
author | Stefan Giehl <stefan@matomo.org> | 2020-04-23 10:43:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-23 10:43:30 +0300 |
commit | a8d917778e75346eab9509ac9707f7e6e2e6c58d (patch) | |
tree | 8259898ab33eb882e631f1f5475cd1482f70e402 /plugins/SegmentEditor | |
parent | 0cdeb6da6e4679c56aa351c004cbd6afe7843c56 (diff) |
Refactores the way segments are configured in dimensions (#15836)
* Improve naming of methods for segments to rescrict access for anonymous user
* Introduce new classes SegmentsList nad DimensionSegmentFactory
* removes SegmentEditor\SegmentList and adds some tests
* submodule updates
Diffstat (limited to 'plugins/SegmentEditor')
4 files changed, 9 insertions, 117 deletions
diff --git a/plugins/SegmentEditor/SegmentFormatter.php b/plugins/SegmentEditor/SegmentFormatter.php index 15de12372c..2825efb738 100644 --- a/plugins/SegmentEditor/SegmentFormatter.php +++ b/plugins/SegmentEditor/SegmentFormatter.php @@ -10,8 +10,6 @@ namespace Piwik\Plugins\SegmentEditor; use Exception; use Piwik\Common; -use Piwik\Config; -use Piwik\Db; use Piwik\Piwik; use Piwik\Segment; use Piwik\Segment\SegmentExpression; @@ -21,7 +19,7 @@ use Piwik\Segment\SegmentExpression; class SegmentFormatter { /** - * @var SegmentList + * @var Segment\SegmentsList */ private $segmentList; @@ -49,7 +47,7 @@ class SegmentFormatter SegmentExpression::BOOL_OPERATOR_END => '', ); - public function __construct(SegmentList $segmentList) + public function __construct(Segment\SegmentsList $segmentList) { $this->segmentList = $segmentList; } @@ -74,14 +72,14 @@ class SegmentFormatter $operand = $expression[SegmentExpression::INDEX_OPERAND]; $name = $operand[SegmentExpression::INDEX_OPERAND_NAME]; - $segment = $this->segmentList->findSegment($name, $idSite); + $segment = $this->segmentList->getSegment($name); if (empty($segment)) { throw new Exception(sprintf("The segment '%s' does not exist.", $name)); } - $readable .= $segment['name'] . ' '; - $readable .= $this->getTranslationForComparison($operand, $segment['type']) . ' '; + $readable .= Piwik::translate($segment->getName()) . ' '; + $readable .= $this->getTranslationForComparison($operand, $segment->getType()) . ' '; $readable .= $this->getFormattedValue($operand); $readable .= $this->getTranslationForBoolOperator($operator) . ' '; } diff --git a/plugins/SegmentEditor/SegmentList.php b/plugins/SegmentEditor/SegmentList.php deleted file mode 100644 index 32bbaf5375..0000000000 --- a/plugins/SegmentEditor/SegmentList.php +++ /dev/null @@ -1,33 +0,0 @@ -<?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\Plugins\SegmentEditor; - -use Piwik\API\Request; -use Piwik\Config; -use Piwik\Db; - -/** - */ -class SegmentList -{ - public function findSegment($segmentName, $idSite) - { - $segments = Request::processRequest('API.getSegmentsMetadata', array( - 'idSites' => array($idSite), - 'filter_limit' => '-1' - )); - - foreach ($segments as $segment) { - if ($segment['segment'] == $segmentName && !empty($segmentName)) { - return $segment; - } - } - } - -} diff --git a/plugins/SegmentEditor/tests/Integration/SegmentFormatterTest.php b/plugins/SegmentEditor/tests/Integration/SegmentFormatterTest.php index a7db9f6666..0e0f250351 100644 --- a/plugins/SegmentEditor/tests/Integration/SegmentFormatterTest.php +++ b/plugins/SegmentEditor/tests/Integration/SegmentFormatterTest.php @@ -9,7 +9,7 @@ namespace Piwik\Plugins\SegmentEditor\tests\Integration; use Piwik\Plugins\SegmentEditor\SegmentFormatter; -use Piwik\Plugins\SegmentEditor\SegmentList; +use Piwik\Segment\SegmentsList; use Piwik\Tests\Framework\Fixture; use Piwik\Tests\Framework\TestCase\IntegrationTestCase; @@ -32,10 +32,10 @@ class SegmentFormatterTest extends IntegrationTestCase { parent::setUp(); - $this->idSite = Fixture::createWebsite('2012-01-01 00:00:00'); - $this->formatter = new SegmentFormatter(new SegmentList()); - Fixture::loadAllTranslations(); + + $this->idSite = Fixture::createWebsite('2012-01-01 00:00:00'); + $this->formatter = new SegmentFormatter(SegmentsList::get()); } public function tearDown(): void diff --git a/plugins/SegmentEditor/tests/Integration/SegmentListTest.php b/plugins/SegmentEditor/tests/Integration/SegmentListTest.php deleted file mode 100644 index 3a66a4bd4e..0000000000 --- a/plugins/SegmentEditor/tests/Integration/SegmentListTest.php +++ /dev/null @@ -1,73 +0,0 @@ -<?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\Plugins\SegmentEditor\tests\Integration; - -use Piwik\Plugins\SegmentEditor\SegmentList; -use Piwik\Tests\Framework\Fixture; -use Piwik\Tests\Framework\Mock\FakeAccess; -use Piwik\Tests\Framework\TestCase\IntegrationTestCase; -use Exception; - -/** - * @group SegmentListTest - * @group SegmentList - * @group SegmentEditor - * @group Plugins - */ -class SegmentListTest extends IntegrationTestCase -{ - /** - * @var SegmentList - */ - private $list; - - private $idSite; - - public function setUp(): void - { - parent::setUp(); - - $this->idSite = Fixture::createWebsite('2012-01-01 00:00:00'); - $this->list = new SegmentList(); - } - - public function test_findSegment_shouldFindSegmentByName_IfNameExists() - { - $segmentName = 'pageUrl'; - - $segment = $this->list->findSegment($segmentName, $this->idSite); - self::assertIsArray($segment); - $this->assertSame($segmentName, $segment['segment']); - } - - public function test_findSegment_shouldNotFindSegmentByName_IfNameDoesNotExist() - { - $segment = $this->list->findSegment('aNyNotExisTinGSegmEnt', $this->idSite); - $this->assertNull($segment); - } - - public function test_findSegment_ShouldThrowException_IfNotEnoughPermission() - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('checkUserHasViewAccess'); - - FakeAccess::clearAccess($superUser = false, array(1)); - - $segment = $this->list->findSegment('pageUrl', 999); - $this->assertNull($segment); - } - - public function provideContainerConfig() - { - return array( - 'Piwik\Access' => new FakeAccess() - ); - } - -} |