Newer
Older
<?php
/**
* Piwik - free/libre analytics platform
*
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\System;
use Piwik\Config;
use Piwik\Option;
use Piwik\Scheduler\Schedule\Schedule;
use Piwik\Scheduler\Task;
use Piwik\Scheduler\Timetable;
use Piwik\SettingsPiwik;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tracker;
const TASKS_FINISHED_OPTION_NAME = "Tests.scheduledTasksFinished";
const TASKS_STARTED_OPTION_NAME = "Tests.scheduledTasksStarted";
$testingEnvironment = new \Piwik_TestingEnvironment();
$testingEnvironment->testCaseClass = null;
$testingEnvironment->addFailingScheduledTask = false;
$testingEnvironment->addScheduledTask = false;
$testingEnvironment->save();
Option::delete(self::TASKS_STARTED_OPTION_NAME);
Option::delete(self::TASKS_FINISHED_OPTION_NAME);
Option::delete(Timetable::TIMETABLE_OPTION_STRING);
SettingsPiwik::overwritePiwikUrl(self::$fixture->getRootUrl() . "tests/PHPUnit/proxy");
}
protected static function configureFixture($fixture)
{
$fixture->createSuperUser = true;
}
* Test the Bulk tracking API as documented in: http://developer.piwik.org/api-reference/tracking-api#bulk-tracking
*
* With invalid token_auth the request would still work
public function test_trackingApiWithBulkRequests_viaCurl_withWrongTokenAuth()
{
$token_auth = '33dc3f2536d3025974cccb4b4d2d98f4';
$this->issueBulkTrackingRequest($token_auth, $expectTrackingToSucceed = true);
}
public function test_trackingApiWithBulkRequests_viaCurl_withCorrectTokenAuth()
$token_auth = Fixture::getTokenAuth();
\Piwik\Filesystem::deleteAllCacheOnUpdate();
$this->issueBulkTrackingRequest($token_auth, $expectTrackingToSucceed = true);
}
public function test_trackingEcommerceOrder_WithHtmlEscapedText_InsertsCorrectLogs()
{
// item sku, item name, item category, item price, item quantity
// NOTE: used to test with '𝌆' character, however, mysql on travis fails with this when
// inserting this character decoded.
$ecItems = array(array('"scarysku', 'superscarymovie"', 'scary & movies', 12.99, 1),
array('> scary', 'but < "super', 'scary"', 14, 15),
array("'Foo ©", " bar ", " baz ☃ qux", 16, 17));
$urlToTest = $this->getEcommerceItemsUrl($ecItems);
$response = $this->sendTrackingRequestByCurl($urlToTest);
Fixture::checkResponse($response);
$this->assertEquals(1, $this->getCountOfConversions());
$conversionItems = $this->getConversionItems();
$this->assertEquals(3, count($conversionItems));
$this->assertActionEquals('"scarysku', $conversionItems[0]['idaction_sku']);
$this->assertActionEquals('superscarymovie"', $conversionItems[0]['idaction_name']);
$this->assertActionEquals('scary & movies', $conversionItems[0]['idaction_category']);
$this->assertActionEquals('> scary', $conversionItems[1]['idaction_sku']);
$this->assertActionEquals('but < "super', $conversionItems[1]['idaction_name']);
$this->assertActionEquals('scary"', $conversionItems[1]['idaction_category']);
$this->assertActionEquals('\'Foo ©', $conversionItems[2]['idaction_sku']);
$this->assertActionEquals('bar', $conversionItems[2]['idaction_name']);
$this->assertActionEquals('baz ☃ qux', $conversionItems[2]['idaction_category']);
public function test_trackingEcommerceOrder_WithAmpersandAndQuotes_InsertsCorrectLogs()
// item sku, item name, item category, item price, item quantity
$ecItems = array(array("\"scarysku&", "superscarymovie'", 'scary <> movies', 12.99, 1));
$urlToTest = $this->getEcommerceItemsUrl($ecItems);
$response = $this->sendTrackingRequestByCurl($urlToTest);
Fixture::checkResponse($response);
$this->assertEquals(1, $this->getCountOfConversions());
$conversionItems = $this->getConversionItems();
$this->assertEquals(1, count($conversionItems));
$this->assertActionEquals('"scarysku&', $conversionItems[0]['idaction_sku']);
$this->assertActionEquals('superscarymovie\'', $conversionItems[0]['idaction_name']);
$this->assertActionEquals('scary <> movies', $conversionItems[0]['idaction_category']);
public function test_trackingEcommerceOrder_DoesNotFail_WhenEmptyEcommerceItemsParamUsed()
// item sku, item name, item category, item price, item quantity
$urlToTest = $this->getEcommerceItemsUrl("");
$response = $this->sendTrackingRequestByCurl($urlToTest);
Fixture::checkResponse($response);
$this->assertEquals(1, $this->getCountOfConversions());
$this->assertEquals(0, count($this->getConversionItems()));
public function test_trackingEcommerceOrder_DoesNotFail_WhenNonArrayUsedWithEcommerceItemsParam()
// item sku, item name, item category, item price, item quantity
$urlToTest = $this->getEcommerceItemsUrl("45");
$response = $this->sendTrackingRequestByCurl($urlToTest);
Fixture::checkResponse($response);
$this->assertEquals(0, $this->getCountOfConversions());
$this->assertEquals(0, count($this->getConversionItems()));
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
public function test_scheduledTasks_CanBeRunThroughTracker_WithoutIncludingOutputInTrackerOutput()
{
$this->setScheduledTasksToRunInTracker();
$urlToTest = $this->getSimpleTrackingUrl();
$response = $this->sendTrackingRequestByCurl($urlToTest);
Fixture::checkResponse($response);
$this->assertScheduledTasksWereRun();
}
public function test_scheduledTasks_CanBeRunThroughTracker_WithOutputIncluded_IfDebugQueryParamUsed()
{
$this->setScheduledTasksToRunInTracker();
$urlToTest = $this->getSimpleTrackingUrl() . '&debug=1';
$response = $this->sendTrackingRequestByCurl($urlToTest);
$this->assertScheduledTasksWereRun();
$this->assertContains('Scheduled Tasks: Starting...', $response);
}
public function getTypesOfErrorsForScheduledTasksTrackerFailureTest()
{
return array(
array(true),
array(false)
);
}
/**
* @dataProvider getTypesOfErrorsForScheduledTasksTrackerFailureTest
*/
public function test_scheduledTasks_DoNotFailTracking_WhenScheduledTaskFails($doFatalError)
{
$this->setScheduledTasksToRunInTracker();
$this->addFailingScheduledTaskToTracker($doFatalError);
$urlToTest = $this->getSimpleTrackingUrl();
$response = $this->sendTrackingRequestByCurl($urlToTest);
Fixture::checkResponse($response);
if ($doFatalError) {
$this->assertScheduledTasksWereNotRun();
} else {
$this->assertScheduledTasksWereRun();
}
}
protected function issueBulkTrackingRequest($token_auth, $expectTrackingToSucceed)
$piwikHost = Fixture::getRootUrl() . 'tests/PHPUnit/proxy/piwik.php';
$command = 'curl -s -X POST -d \'{"requests":["?idsite=1&url=http://example.org&action_name=Test bulk log Pageview&rec=1","?idsite=1&url=http://example.net/test.htm&action_name=Another bulk page view&rec=1"],"token_auth":"' . $token_auth . '"}\' ' . $piwikHost;
exec($command, $output, $result);
if ($result !== 0) {
throw new \Exception("tracking bulk failed: " . implode("\n", $output) . "\n\ncommand used: $command");
}
$output = implode("", $output);
$this->assertStringStartsWith('{"status":', $output);
if($expectTrackingToSucceed) {
$this->assertNotContains('error', $output);
$this->assertContains('success', $output);
} else {
$this->assertContains('error', $output);
$this->assertNotContains('success', $output);
}
if (!function_exists('curl_init')) {
$this->markTestSkipped('Curl is not installed');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, Fixture::getRootUrl() . 'tests/PHPUnit/proxy/piwik.php' . $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$response = substr($response, $headerSize);
curl_close($ch);
private function assertActionEquals($expected, $idaction)
$actionName = Db::fetchOne("SELECT name FROM " . Common::prefixTable('log_action') . " WHERE idaction = ?", array($idaction));
$this->assertEquals($expected, $actionName);
Thomas Steur
a validé
{
return Db::fetchOne("SELECT COUNT(*) FROM " . Common::prefixTable('log_conversion'));
}
Thomas Steur
a validé
private function getConversionItems()
{
return Db::fetchAll("SELECT * FROM " . Common::prefixTable('log_conversion_item'));
Thomas Steur
a validé
}
private function getEcommerceItemsUrl($ecItems, $doJsonEncode = true)
{
$ecItemsStr = $doJsonEncode ? json_encode($ecItems) : $ecItems;
return "?idsite=1&idgoal=0&rec=1&url=" . urlencode('http://quellehorreur.com/movies') . "&ec_items="
. urlencode($ecItemsStr) . '&ec_id=myspecial-id-1234&revenue=16.99&ec_st=12.99&ec_tx=0&ec_sh=3';
}
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
private function getSimpleTrackingUrl()
{
return "?idsite=1&rec=1&url=" . urlencode('http://quellehorreur.com/movies') . "&action_name=Movies";
}
private function setScheduledTasksToRunInTracker()
{
$testingEnvironment = new \Piwik_TestingEnvironment();
$testingEnvironment->testCaseClass = 'Piwik\Tests\System\TrackerTest';
$testingEnvironment->addScheduledTask = true;
$testingEnvironment->configOverride = array('Tracker' => array('scheduled_tasks_min_interval' => 1, 'debug_on_demand' => 1));
$testingEnvironment->save();
}
private function addFailingScheduledTaskToTracker($doFatalError)
{
$testingEnvironment = new \Piwik_TestingEnvironment();
$testingEnvironment->addFailingScheduledTask = true;
$testingEnvironment->scheduledTaskFailureShouldBeFatal = $doFatalError;
$testingEnvironment->save();
}
public function provideContainerConfig()
{
define('DEBUG_FORCE_SCHEDULED_TASKS', 1);
$testingEnvironment = new \Piwik_TestingEnvironment();
$tasksToAdd = array();
if ($testingEnvironment->addFailingScheduledTask) {
if ($testingEnvironment->scheduledTaskFailureShouldBeFatal) {
$tasksToAdd[] = new Task($this, 'triggerFatalError', null, Schedule::factory('hourly'));
} else {
$tasksToAdd[] = new Task($this, 'throwScheduledTaskException', null, Schedule::factory('hourly'));
}
}
if ($testingEnvironment->addScheduledTask) {
$tasksToAdd[] = new Task($this, 'markScheduledTaskExecutionFinished', null, Schedule::factory('hourly'));
}
$result = array();
if (!empty($tasksToAdd)) {
$initialTask = new Task($this, 'markCustomTaskExecuted', null, Schedule::factory('hourly'));
$tasksToAdd = array_merge(array($initialTask), $tasksToAdd);
$mockTaskLoader = $this->getMock('Piwik\Scheduler\TaskLoader', array('loadTasks'));
$mockTaskLoader->expects($this->any())->method('loadTasks')->will($this->returnValue($tasksToAdd));
$result['Piwik\Scheduler\TaskLoader'] = $mockTaskLoader;
}
return $result;
}
public function triggerFatalError()
{
require 'thehowling';
}
public function throwScheduledTaskException()
{
throw new \Exception("triggered exception");
}
public function markScheduledTaskExecutionFinished()
{
Option::set(self::TASKS_FINISHED_OPTION_NAME, 1);
}
public function markCustomTaskExecuted()
{
Option::set(self::TASKS_STARTED_OPTION_NAME, 1);
}
private function assertScheduledTasksWereRun()
{
$this->assertCustomTasksWereStarted();
Option::clearCachedOption(self::TASKS_FINISHED_OPTION_NAME);
$this->assertEquals(1, Option::get(self::TASKS_FINISHED_OPTION_NAME));
}
private function assertScheduledTasksWereNotRun()
{
$this->assertCustomTasksWereStarted();
Option::clearCachedOption(self::TASKS_FINISHED_OPTION_NAME);
$this->assertEmpty(Option::get(self::TASKS_FINISHED_OPTION_NAME));
}
private function assertCustomTasksWereStarted()
{
Option::clearCachedOption(self::TASKS_STARTED_OPTION_NAME);
$this->assertEquals(1, Option::get(self::TASKS_STARTED_OPTION_NAME));
}