Newer
Older
Thomas Steur
a validé
<?php
/**
* Piwik - free/libre analytics platform
Thomas Steur
a validé
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Insights\tests;
use Piwik\DataTable;
use Piwik\Plugins\Insights\Model;
use Piwik\Plugins\Insights\tests\Fixtures\SomeVisitsDifferentPathsOnTwoDays;
Thomas Steur
a validé
use Piwik\Tests\Framework\TestCase\SystemTestCase;
Thomas Steur
a validé
/**
* @group Insights
* @group ModelTest
Thomas Steur
a validé
* @group Plugins
Thomas Steur
a validé
* @group Plugins
*/
class ModelTest extends SystemTestCase
Thomas Steur
a validé
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
{
/**
* @var SomeVisitsDifferentPathsOnTwoDays
*/
public static $fixture = null;
/**
* @var Model
*/
private $model;
public function setUp()
{
parent::setUp();
$this->model = new Model();
}
public function test_requestReport_shouldReturnTheDataTableOfTheReport_AndContainReportTotals()
{
$idSite = self::$fixture->idSite;
$date = self::$fixture->date1;
$metric = 'nb_visits';
$table = $this->model->requestReport($idSite, 'day', $date, 'Actions_getPageUrls', $metric, false);
$this->assertEquals(5, $table->getRowsCount());
$totals = $table->getMetadata('totals');
$this->assertEquals(50, $totals[$metric]);
}
public function test_getReportByUniqueId_shouldReturnReport()
{
$report = $this->model->getReportByUniqueId(self::$fixture->idSite, 'Actions_getPageUrls');
$this->assertEquals('Actions', $report['module']);
$this->assertEquals('getPageUrls', $report['action']);
}
public function test_getLastDate_shouldReturnTheLastDateDependingOnPeriod()
{
$date = $this->model->getLastDate('2012-12-12', 'day', 1);
$this->assertEquals('2012-12-11', $date);
$date = $this->model->getLastDate('2012-12-12', 'week', 1);
$this->assertEquals('2012-12-05', $date);
$date = $this->model->getLastDate('2012-12-03', 'week', 1);
$this->assertEquals('2012-11-26', $date);
$date = $this->model->getLastDate('2012-12-02', 'week', 1);
$this->assertEquals('2012-11-25', $date);
$date = $this->model->getLastDate('2012-12-12', 'month', 1);
$this->assertEquals('2012-11-12', $date);
$date = $this->model->getLastDate('2012-12-01', 'month', 1);
$this->assertEquals('2012-11-01', $date);
}
public function test_getLastDate_shouldReturnTheLastDateDependingOnComparedTo()
{
$date = $this->model->getLastDate('2012-12-12', 'day', 1);
$this->assertEquals('2012-12-11', $date);
$date = $this->model->getLastDate('2012-12-12', 'day', 2);
$this->assertEquals('2012-12-10', $date);
$date = $this->model->getLastDate('2012-12-12', 'day', 7);
$this->assertEquals('2012-12-05', $date);
}
public function test_getMetricTotalValue_shouldReturnTheTotalValueFromMetadata()
{
$table = $this->getTableWithTotal('17');
$total = $this->model->getMetricTotalValue($table, 'nb_visits');
$this->assertEquals(17, $total);
$this->assertInternalType('integer', $total);
}
public function test_getMetricTotalValue_shouldReturnZeroIfMetricHasNoTotal()
{
$table = new DataTable();
$table->setMetadata('totals', array('nb_visits' => '17'));
$total = $this->model->getMetricTotalValue($table, 'unknown_metric');
$this->assertEquals(0, $total);
}
/**
* @expectedException \Exception
*/
public function test_getLastDate_shouldThrowExceptionIfNotPossibleToGetLastDate()
{
$this->model->getLastDate('last10', 'day', 1);
}
Thomas Steur
a validé
/**
* @expectedException \Exception
*/
public function test_getLastDate_shouldThrowExceptionInCaseOfRangePeriod()
{
$this->model->getLastDate('2012-11-11,2012-12-12', 'range', 1);
}
Thomas Steur
a validé
public function test_getTotalValue_shouldCalculateTotals()
{
Thomas Steur
a validé
$total = $this->model->getTotalValue(self::$fixture->idSite, 'day', self::$fixture->date1, 'nb_visits', false);
Thomas Steur
a validé
$this->assertEquals(50, $total);
Thomas Steur
a validé
$total = $this->model->getTotalValue(self::$fixture->idSite, 'day', self::$fixture->date2, 'nb_visits', false);
Thomas Steur
a validé
$this->assertEquals(59, $total);
}
Thomas Steur
a validé
public function test_getTotalValue_shouldCalculateTotalsAndApplySegment()
{
$total = $this->model->getTotalValue(self::$fixture->idSite, 'day', self::$fixture->date1, 'nb_visits', 'resolution==1000x1001');
Thomas Steur
a validé
$this->assertEquals(1, $total);
}
Thomas Steur
a validé
public function test_getTotalValue_shouldReturnZero_IfColumnDoesNotExist()
{
$total = $this->model->getTotalValue(self::$fixture->idSite, 'day', self::$fixture->date1, 'unknown_ColUmn', false);
$this->assertEquals(0, $total);
Thomas Steur
a validé
}
public function test_getRelevantTotalValue_shouldReturnTotalValue_IfMetricTotalIsHighEnough()
{
$table = $this->getTableWithTotal(25);
Thomas Steur
a validé
$total = $this->model->getRelevantTotalValue($table, 'nb_visits', 50);
Thomas Steur
a validé
$this->assertEquals(50, $total);
}
public function test_getRelevantTotalValue_shouldReturnMetricTotal_IfMetricTotalIsHigherThanTotalValue()
{
$table = $this->getTableWithTotal(80);
Thomas Steur
a validé
$total = $this->model->getRelevantTotalValue($table, 'nb_visits', 50);
Thomas Steur
a validé
$this->assertEquals(80, $total);
}
public function test_getRelevantTotalValue_shouldReturnMetricTotal_IfMetricTotalIsTooLow()
{
$table = $this->getTableWithTotal(24);
Thomas Steur
a validé
$total = $this->model->getRelevantTotalValue($table, 'nb_visits', 50);
Thomas Steur
a validé
$this->assertEquals(24, $total);
$table = $this->getTableWithTotal(0);
Thomas Steur
a validé
$total = $this->model->getRelevantTotalValue($table, 'nb_visits', 50);
Thomas Steur
a validé
$this->assertEquals(0, $total);
}
private function getTableWithTotal($total)
{
$table = new DataTable();
$table->setMetadata('totals', array('nb_visits' => $total));
return $table;
}
}
ModelTest::$fixture = new SomeVisitsDifferentPathsOnTwoDays();