Skip to content

Commit

Permalink
added some tests for segment formatting and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Nov 23, 2015
1 parent 4f794ec commit d79b2d3
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 16 deletions.
41 changes: 36 additions & 5 deletions plugins/SegmentEditor/SegmentFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
*/
namespace Piwik\Plugins\SegmentEditor;

use Exception;
use Piwik\Config;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Segment;
use Piwik\Segment\SegmentExpression;

/**
Expand All @@ -36,8 +38,8 @@ class SegmentFormatter
SegmentExpression::MATCH_NOT_EQUAL => 'General_OperationIsNot',
SegmentExpression::MATCH_CONTAINS => 'General_OperationContains',
SegmentExpression::MATCH_DOES_NOT_CONTAIN => 'General_OperationDoesNotContain',
'=^' => 'General_OperationStartsWith',
'=$' => 'General_OperationEndsWith'
SegmentExpression::MATCH_STARTS_WITH => 'General_OperationStartsWith',
SegmentExpression::MATCH_ENDS_WITH => 'General_OperationEndsWith'
);

private $operators = array(
Expand All @@ -64,19 +66,22 @@ public function getHumanReadable($segmentString, $idSite)
foreach ($expressions as $expression) {
$operator = $expression[SegmentExpression::INDEX_BOOL_OPERATOR];
$operand = $expression[SegmentExpression::INDEX_OPERAND];
$name = $operand[SegmentExpression::INDEX_OPERAND_NAME];

$segment = $this->segmentList->findSegment($operand[SegmentExpression::INDEX_OPERAND_NAME], $idSite);
$segment = $this->segmentList->findSegment($name, $idSite);

if (empty($segment)) {
continue;
throw new Exception(sprintf("The segment '%s' does not exist.", $name));
}

$readable .= $segment['name'] . ' ';
$readable .= $this->getTranslationForComparison($operand, $segment['type']) . ' ';
$readable .= '"' . $operand[SegmentExpression::INDEX_OPERAND_VALUE] . '" ';
$readable .= $this->getFormattedValue($operand);
$readable .= $this->getTranslationForBoolOperator($operator) . ' ';
}

$readable = trim($readable);

return $readable;
}

Expand All @@ -86,6 +91,14 @@ private function getTranslationForComparison($operand, $segmentType)

$translation = $operator;

if ($operator === SegmentExpression::MATCH_IS_NULL_OR_EMPTY) {
return Piwik::translate('SegmentEditor_SegmentOperatorIsNullOrEmpty');
}

if ($operator === SegmentExpression::MATCH_IS_NOT_NULL_NOR_EMPTY) {
return Piwik::translate('SegmentEditor_SegmentOperatorIsNotNullNOrEmpty');
}

if ($segmentType === 'dimension' && !empty($this->matchesDimension[$operator])) {
$translation = Piwik::translate($this->matchesDimension[$operator]);
}
Expand All @@ -96,6 +109,24 @@ private function getTranslationForComparison($operand, $segmentType)
return strtolower($translation);
}

private function getFormattedValue($operand)
{
$operator = $operand[SegmentExpression::INDEX_OPERAND_OPERATOR];

if ($operator === SegmentExpression::MATCH_IS_NULL_OR_EMPTY
|| $operator === SegmentExpression::MATCH_IS_NOT_NULL_NOR_EMPTY) {
return '';
}

$value = $operand[SegmentExpression::INDEX_OPERAND_VALUE];

if (empty($value)) {
$value = '';
}

return '"' . $value . '" ';
}

private function getTranslationForBoolOperator($operator)
{
$translation = '';
Expand Down
5 changes: 5 additions & 0 deletions plugins/SegmentEditor/SegmentSelectorControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
*/
namespace Piwik\Plugins\SegmentEditor;

use Piwik\API\Request;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugins\API\API as APIMetadata;
use Piwik\View\UIControl;
Expand Down Expand Up @@ -37,6 +39,9 @@ public function __construct($idSite = false)

$this->selectedSegment = Common::getRequestVar('segment', false, 'string');

$formatter = StaticContainer::get('Piwik\Plugins\SegmentEditor\SegmentFormatter');
$this->segmentDescription = $formatter->getHumanReadable(Request::getRawSegmentFromRequest(), $this->idSite);

$this->isAddingSegmentsForAllWebsitesEnabled = SegmentEditor::isAddingSegmentsForAllWebsitesEnabled();

$segments = APIMetadata::getInstance()->getSegmentsMetadata($this->idSite);
Expand Down
6 changes: 4 additions & 2 deletions plugins/SegmentEditor/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"AreYouSureDeleteSegment": "Are you sure you want to delete this segment?",
"AutoArchivePreProcessed": "segmented reports are pre-processed (faster, requires cron)",
"AutoArchiveRealTime": "segmented reports are processed in real time",
"ChooseASegment": "Choose a segment",
"ChooseASegment": "Choose a segment, currently selected segment: %s",
"DataAvailableAtLaterDate": "Your segmented analytics reports will be available later. We apologize for the inconvenience.",
"DefaultAllVisits": "All visits",
"DragDropCondition": "Drag & Drop condition",
Expand All @@ -28,6 +28,8 @@
"YouDontHaveAccessToCreateSegments": "You don't have the required access level to create and edit segments.",
"AddingSegmentForAllWebsitesDisabled": "Adding segments for all websites has been disabled.",
"SegmentXIsAUnionOf": "%s is a union of these segments:",
"CustomSegment": "Custom Segment"
"CustomSegment": "Custom Segment",
"SegmentOperatorIsNullOrEmpty": "is null or empty",
"SegmentOperatorIsNotNullNOrEmpty": "is not null nor empty"
}
}
2 changes: 1 addition & 1 deletion plugins/SegmentEditor/templates/_segmentSelector.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="SegmentEditor" style="display:none;">
<div class="segmentationContainer listHtml" title="{{ 'SegmentEditor_ChooseASegment'|translate|e('html_attr') }}">
<div class="segmentationContainer listHtml" title="{{ 'SegmentEditor_ChooseASegment'|translate(segmentDescription)|e('html_attr') }}">
<a class="title"><span class="icon icon-segment"></span><span class="segmentationTitle"></span></a>
<div class="dropdown dropdown-body">
<div class="segmentFilterContainer">
Expand Down
110 changes: 110 additions & 0 deletions plugins/SegmentEditor/tests/Integration/SegmentFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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\SegmentEditor\tests\Integration;

use Piwik\Plugins\SegmentEditor\SegmentFormatter;
use Piwik\Plugins\SegmentEditor\SegmentList;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Translate;
use Exception;

/**
* @group SegmentFormatterTest
* @group SegmentFormatter
* @group SegmentEditor
* @group Plugins
*/
class SegmentFormatterTest extends IntegrationTestCase
{
/**
* @var SegmentFormatter
*/
private $formatter;

private $idSite;

public function setUp()
{
parent::setUp();

$this->idSite = Fixture::createWebsite('2012-01-01 00:00:00');
$this->formatter = new SegmentFormatter(new SegmentList());

Translate::loadAllTranslations();
}

public function tearDown()
{
Translate::reset();
}

public function test_getHumanReadable_noSegmentGiven_ShouldReturnDefaultSegment()
{
$readable = $this->formatter->getHumanReadable($segment = '', $this->idSite);
$this->assertSame('All visits', $readable);
}

public function test_getHumanReadable_ShouldTranslateAMetric()
{
$readable = $this->formatter->getHumanReadable($segment = 'visitCount>5', $this->idSite);
$this->assertSame('Number of visits greater than "5"', $readable);

$readable = $this->formatter->getHumanReadable($segment = 'visitCount==5', $this->idSite);
$this->assertSame('Number of visits equals "5"', $readable);
}

public function test_getHumanReadable_ShouldTranslateADimension()
{
$readable = $this->formatter->getHumanReadable($segment = 'resolution=@1024', $this->idSite);
$this->assertSame('Resolution contains "1024"', $readable);

$readable = $this->formatter->getHumanReadable($segment = 'resolution==1024x768', $this->idSite);
$this->assertSame('Resolution is "1024x768"', $readable);
}

public function test_getHumanReadable_ShouldCombineMultipleSegmentDefinitionsWithBooleanOperator()
{
$readable = $this->formatter->getHumanReadable($segment = 'browserVersion!=1.0;browserEngine=$Trident', $this->idSite);
$this->assertSame('Browser version is not "1.0" and Browser engine ends with "Trident"', $readable);

$readable = $this->formatter->getHumanReadable($segment = 'browserVersion!=1.0,browserEngine=$Trident', $this->idSite);
$this->assertSame('Browser version is not "1.0" or Browser engine ends with "Trident"', $readable);
}

public function test_getHumanReadable_ShouldHandleAMissingValue()
{
$readable = $this->formatter->getHumanReadable($segment = 'browserVersion==', $this->idSite);
$this->assertSame('Browser version is null or empty', $readable);

$readable = $this->formatter->getHumanReadable($segment = 'browserVersion!=', $this->idSite);
$this->assertSame('Browser version is not null nor empty', $readable);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage The segment 'noTexisTinG' does not exist
*/
public function test_getHumanReadable_ShouldThrowAnException_IfTheGivenSegmentNameDoesNotExist()
{
$this->formatter->getHumanReadable($segment = 'noTexisTinG==1.0', $this->idSite);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage The segment 'pageUrl=!1.0' is not valid.
*/
public function test_getHumanReadable_ShouldThrowAnException_IfSegmentCannotBeParsedBecauseOfInvalidFormat()
{
$invalidOperator = '=!';
$this->formatter->getHumanReadable($segment = 'pageUrl' . $invalidOperator . '1.0', $this->idSite);
}

}
7 changes: 0 additions & 7 deletions plugins/SegmentEditor/tests/Integration/SegmentListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@

namespace Piwik\Plugins\SegmentEditor\tests\Integration;

use Piwik\Date;
use Piwik\Piwik;
use Piwik\Plugins\SegmentEditor\API;
use Piwik\Plugins\SegmentEditor\Model;
use Piwik\Plugins\SegmentEditor\SegmentList;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Exception;

/**
* Class Plugins_SegmentEditorTest
*
* @group SegmentListTest
* @group SegmentList
* @group SegmentEditor
Expand Down
2 changes: 1 addition & 1 deletion tests/UI/expected-ui-screenshots

0 comments on commit d79b2d3

Please sign in to comment.