public function testWebArchiving() { if (self::isMysqli() && self::isTravisCI()) { $this->markTestSkipped('Skipping on Mysqli as it randomly fails.'); } $host = Fixture::getRootUrl(); $token = Fixture::getTokenAuth(); $urlTmp = Option::get('piwikUrl'); Option::set('piwikUrl', $host . 'tests/PHPUnit/proxy/index.php'); $url = $host . 'tests/PHPUnit/proxy/archive.php?token_auth=' . $token; $output = Http::sendHttpRequest($url, 600); // ignore random build issues if (empty($output) || strpos($output, \Piwik\CronArchive::NO_ERROR) === false) { $message = "This test has failed. Because it sometimes randomly fails, we skip the test, and ignore this failure.\n"; $message .= "If you see this message often, or in every build, please investigate as this should only be a random and rare occurence!\n"; $message .= "\n\narchive web failed: " . $output . "\n\nurl used: {$url}"; $this->markTestSkipped($message); } if (!empty($urlTmp)) { Option::set('piwikUrl', $urlTmp); } else { Option::delete('piwikUrl'); } $this->assertContains('Starting Piwik reports archiving...', $output); $this->assertContains('Archived website id = 1', $output); $this->assertContains('Done archiving!', $output); $this->compareArchivePhpOutputAgainstExpected($output); }
private function buildTracker() { $t = Fixture::getTracker($this->idSite, Date::factory('2014-01-05 00:01:01')->getDatetime()); $t->setDebugStringAppend('debug=1'); $t->setTokenAuth(Fixture::getTokenAuth()); $t->setUrl('http://example.org/index1.htm'); return $t; }
public function setUp() { parent::setUp(); $this->cliMulti = new CliMulti(); $this->authToken = Fixture::getTokenAuth(); $this->urls = array('getAnswerToLife' => $this->completeUrl('?module=API&method=ExampleAPI.getAnswerToLife&format=JSON'), 'getPiwikVersion' => $this->completeUrl('?module=API&method=API.getPiwikVersion&format=JSON')); $this->responses = array('getAnswerToLife' => '{"value":42}', 'getPiwikVersion' => '{"value":"' . Version::VERSION . '"}'); \Piwik\Common::$isCliMode = true; }
public function test_getBulkRequest_IsAbleToHandleManyDifferentRequests() { $token = Fixture::getTokenAuth(); $urls = array("method%3dVisitsSummary.get%26idSite%3d1%26date%3d2015-01-26%26period%3dday", "method%3dVisitsSummary.get%26token_auth%3d{$token}%26idSite%3d1%26date%3d2015-01-26%26period%3dday", "method%3dVisitsSummary.get%26idSite%3d1%26date%3d2015-01-26%26period%3dday", "method%3dVisitsSummary.get%26idSite%3d1%26token_auth%3danonymous%26date%3d2015-01-26%26period%3dday"); $response = $this->api->getBulkRequest($urls); $this->assertResponseIsPermissionError($response[0]); $this->assertResponseIsSuccess($response[1]); $this->assertSame(0, $response[1]['nb_visits']); $this->assertResponseIsPermissionError($response[2]); $this->assertResponseIsPermissionError($response[3]); }
/** * Creates a tracking object that invokes the tracker directly (w/o going through HTTP). */ public static function getLocalTracker($idSite) { require_once PIWIK_INCLUDE_PATH . '/tests/LocalTracker.php'; $t = new \Piwik_LocalTracker($idSite, Fixture::getTrackerUrl()); $t->setUserAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)"); $t->setBrowserLanguage('fr'); $t->setLocalTime('12:34:06'); $t->setResolution(1024, 768); $t->setBrowserHasCookies(true); $t->setPlugins($flash = true, $java = true, $director = false); $t->setTokenAuth(Fixture::getTokenAuth()); return $t; }
public function test_LogImporter_RetriesWhenServerFails() { $this->simulateTrackerFailure(); $logFile = PIWIK_INCLUDE_PATH . '/tests/resources/access-logs/fake_logs_enable_all.log'; $options = array('--idsite' => self::$fixture->idSite, '--token-auth' => Fixture::getTokenAuth(), '--retry-max-attempts' => 5, '--retry-delay' => 1); $output = Fixture::executeLogImporter($logFile, $options, $allowFailure = true); $output = implode("\n", $output); for ($i = 2; $i != 6; ++$i) { $this->assertContains("Retrying request, attempt number {$i}", $output); } $this->assertNotContains("Retrying request, attempt number 6", $output); $this->assertContains("Max number of attempts reached, server is unreachable!", $output); }
public function test_WebArchiving() { if (self::isMysqli() && self::isTravisCI()) { $this->markTestSkipped('Skipping on Mysqli as it randomly fails.'); } $host = Fixture::getRootUrl(); $token = Fixture::getTokenAuth(); $urlTmp = Option::get('piwikUrl'); Option::set('piwikUrl', $host . 'tests/PHPUnit/proxy/index.php'); $url = $host . 'tests/PHPUnit/proxy/archive.php?token_auth=' . $token; $output = Http::sendHttpRequest($url, 600); // ignore random build issues if (empty($output) || strpos($output, \Piwik\CronArchive::NO_ERROR) === false) { $this->fail("archive web failed: " . $output . "\n\nurl used: {$url}"); } if (!empty($urlTmp)) { Option::set('piwikUrl', $urlTmp); } else { Option::delete('piwikUrl'); } $this->assertWebArchivingDone($output); }
private function assertRememberedArchivedReportsThatShouldBeInvalidated($idsite, $requestDate, $expectedRemeberedArchivedReports) { /** @var Visit $visit */ list($visit) = $this->prepareVisitWithRequest(array('idsite' => $idsite, 'rec' => 1, 'cip' => '156.146.156.146', 'token_auth' => Fixture::getTokenAuth()), $requestDate); $visit->handle(); $archive = StaticContainer::get('Piwik\\Archive\\ArchiveInvalidator'); $remembered = $archive->getRememberedArchivedReportsThatShouldBeInvalidated(); $this->assertSame($expectedRemeberedArchivedReports, $remembered); }
private function getTracker($hoursOffset = 0) { $hour = 10; if ($hoursOffset) { $hour += $hoursOffset; } $date = sprintf('%s %d:20:01', $this->date, $hour); $tracker = Fixture::getTracker(1, $date, true, true); $tracker->setTokenAuth(Fixture::getTokenAuth()); return $tracker; }
public function test_main_shouldReportInvalidIndices_IfInvalidRequestsIncluded_AndRequestAuthenticated() { $this->injectRawDataToBulk($this->getDummyRequest($token = Fixture::getTokenAuth(), $idSite = array(1, -100))); $handler = $this->getHandler(); $handler->setResponse(new Response()); $response = $this->tracker->main($handler, $this->getEmptyRequestSet()); $this->assertEquals('{"status":"success","tracked":1,"invalid":1,"invalid_indices":[1]}', $response); }
/** * While {@link runApiTests()} lets you run test for many API methods at once this one tests only one specific * API method and it goes via HTTP. While the other method lets you test only some methods starting with 'get' * this one lets you actually test any API method. */ protected function runAnyApiTest($apiMethod, $apiId, $requestParams, $options = array()) { $requestParams['module'] = 'API'; $requestParams['format'] = 'XML'; $requestParams['method'] = $apiMethod; $apiId = $apiMethod . '_' . $apiId . '.xml'; $testName = 'test_' . static::getOutputPrefix(); list($processedFilePath, $expectedFilePath) = $this->getProcessedAndExpectedPaths($testName, $apiId, $format = null, $compareAgainst = false); if (!array_key_exists('token_auth', $requestParams)) { $requestParams['token_auth'] = Fixture::getTokenAuth(); } $response = $this->getResponseFromHttpAPI($requestParams); $processedResponse = new Response($response, $options, $requestParams); if (empty($compareAgainst)) { $processedResponse->save($processedFilePath); } try { $expectedResponse = Response::loadFromFile($expectedFilePath, $options, $requestParams); } catch (Exception $ex) { $this->handleMissingExpectedFile($expectedFilePath, $processedResponse); return; } try { $errorMessage = get_class($this) . ": Differences with expected in '{$processedFilePath}'"; Response::assertEquals($expectedResponse, $processedResponse, $errorMessage); } catch (Exception $ex) { $this->comparisonFailures[] = $ex; } $this->printApiTestFailures(); }
private function buildRequestSetWithIdSite($numRequests, $additionalParams = array()) { $req = new RequestSet(); $requests = array(); for ($index = 1; $index <= $numRequests; $index++) { $requests[] = array_merge(array('idsite' => 1, 'cip' => '192.168.33.11', 'token_auth' => Fixture::getTokenAuth()), $additionalParams); } $req->setRequests($requests); $req->rememberEnvironment(); return $req; }
private function getTracker() { $tracker = Fixture::getTracker(1, self::FIRST_VISIT_TIME, $defaultInit = true, $useLocalTracker = true); $tracker->setTokenAuth(Fixture::getTokenAuth()); // properties that cannot be changed on next action $tracker->setUserAgent(self::TEST_USER_AGENT); $tracker->setBrowserLanguage(self::TEST_BROWSER_LANGUAGE); // properties that can be changed on next action $tracker->setCountry(self::TEST_COUNTRY); $tracker->setRegion(self::TEST_REGION); return $tracker; }
private function setDummyRequests($useTokenAuth = true) { if ($useTokenAuth) { $useTokenAuth = Fixture::getTokenAuth(); } $this->requestSet->setRequests(array(array('idsite' => 1, 'url' => 'http://localhost/foo?bar', 'cip' => '192.168.33.11', 'token_auth' => $useTokenAuth), array('idsite' => 1, 'url' => 'http://localhost', 'cip' => '192.168.33.11', 'token_auth' => $useTokenAuth))); }
public function test_authenticateSuperUserOrAdmin_ShouldAlwaysWorkForSuperUser() { Fixture::createSuperUser(false); $token = Fixture::getTokenAuth(); $isAuthenticated = Request::authenticateSuperUserOrAdmin($token, 1); $this->assertTrue($isAuthenticated); $isAuthenticated = Request::authenticateSuperUserOrAdmin($token, 2); $this->assertTrue($isAuthenticated); }
public function test_trackingApiWithBulkRequests_viaCurl_withCorrectTokenAuth() { $token_auth = Fixture::getTokenAuth(); \Piwik\Filesystem::deleteAllCacheOnUpdate(); $this->issueBulkTrackingRequest($token_auth, $expectTrackingToSucceed = true); }
private function getTracker() { $tracker = Fixture::getTracker(1, $this->date . ' 00:01:01', true, true); $tracker->setTokenAuth(Fixture::getTokenAuth()); return $tracker; }
private function getSuperUserToken() { Fixture::createSuperUser(false); return Fixture::getTokenAuth(); }
private function sendArchiveWebRequest() { return $this->curl(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/archive.php?token_auth=' . Fixture::getTokenAuth()); }
/** Creates two dashboards that split the widgets up into different groups. */ public function setupDashboards() { $dashboardColumnCount = 3; $dashboardCount = 4; $layout = array(); for ($j = 0; $j != $dashboardColumnCount; ++$j) { $layout[] = array(); } $dashboards = array(); for ($i = 0; $i != $dashboardCount; ++$i) { $dashboards[] = $layout; } $oldGet = $_GET; $_GET['idSite'] = 1; $_GET['token_auth'] = Fixture::getTokenAuth(); // collect widgets & sort them so widget order is not important $allWidgets = array(); foreach (WidgetsList::get() as $category => $widgets) { $allWidgets = array_merge($allWidgets, $widgets); } usort($allWidgets, function ($lhs, $rhs) { return strcmp($lhs['uniqueId'], $rhs['uniqueId']); }); $widgetsPerDashboard = ceil(count($allWidgets) / $dashboardCount); // group widgets so they will be spread out across 3 dashboards $groupedWidgets = array(); $dashboard = 0; foreach ($allWidgets as $widget) { if ($widget['uniqueId'] == 'widgetSEOgetRank' || $widget['uniqueId'] == 'widgetReferrersgetKeywordsForPage' || $widget['uniqueId'] == 'widgetLivegetVisitorProfilePopup' || $widget['uniqueId'] == 'widgetActionsgetPageTitles' || strpos($widget['uniqueId'], 'widgetExample') === 0) { continue; } $widgetEntry = array('uniqueId' => $widget['uniqueId'], 'parameters' => $widget['parameters']); // dashboard images must have height of less than 4000px to avoid odd discoloration of last line of image $widgetEntry['parameters']['filter_limit'] = 5; $groupedWidgets[$dashboard][] = $widgetEntry; if (count($groupedWidgets[$dashboard]) >= $widgetsPerDashboard) { $dashboard = $dashboard + 1; } // sanity check if ($dashboard >= $dashboardCount) { throw new Exception("Unexpected error: Incorrect dashboard widget placement logic. Something's wrong w/ the code."); } } // distribute widgets in each dashboard $column = 0; foreach ($groupedWidgets as $dashboardIndex => $dashboardWidgets) { foreach ($dashboardWidgets as $widget) { $column = ($column + 1) % $dashboardColumnCount; $dashboards[$dashboardIndex][$column][] = $widget; } } foreach ($dashboards as $id => $layout) { if ($id == 0) { $_GET['name'] = self::makeXssContent('dashboard name' . $id); } else { $_GET['name'] = 'dashboard name' . $id; } $_GET['layout'] = json_encode($layout); $_GET['idDashboard'] = $id + 1; FrontController::getInstance()->fetchDispatch('Dashboard', 'saveLayout'); } // create empty dashboard $dashboard = array(array(array('uniqueId' => "widgetVisitsSummarygetEvolutionGraphcolumnsArray", 'parameters' => array('module' => 'VisitsSummary', 'action' => 'getEvolutionGraph', 'columns' => 'nb_visits'))), array(), array()); $_GET['name'] = 'D4'; $_GET['layout'] = json_encode($dashboard); $_GET['idDashboard'] = 5; $_GET['idSite'] = 2; FrontController::getInstance()->fetchDispatch('Dashboard', 'saveLayout'); $_GET = $oldGet; }
protected static function _addLogData() { // tracks visits on the following days: // - 2012-01-09 // - 2012-01-14 // - 2012-01-19 // - 2012-01-24 <--- everything before this date is to be purged // - 2012-01-29 // - 2012-02-03 // - 2012-02-08 // - 2012-02-13 // - 2012-02-18 // - 2012-02-23 // - 2012-02-28 // 6 visits in feb, 5 in jan // following actions are created: // - 'First page view' // - 'Second page view' // - 'SKU2' // - 'Canon SLR' // - 'Electronics & Cameras' // - for every visit (11 visits total): // - http://whatever.com/_{$daysSinceLastVisit} // - http://whatever.com/42/{$daysSinceLastVisit} $start = Date::factory(self::$dateTime); self::$idSite = Fixture::createWebsite('2012-01-01', $ecommerce = 1); APIGoals::getInstance()->addGoal(self::$idSite, 'match all', 'url', 'http', 'contains'); $t = Fixture::getTracker(self::$idSite, $start, $defaultInit = true); $t->enableBulkTracking(); $t->setTokenAuth(Fixture::getTokenAuth()); for ($daysAgo = self::$daysAgoStart; $daysAgo >= 0; $daysAgo -= 5) { $dateTime = $start->subDay($daysAgo)->toString(); $t->setForceVisitDateTime($dateTime); $t->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)'); // use $daysAgo to make sure new actions are created for every day and aren't used again. // when deleting visits, some of these actions will no longer be referenced in the DB. $t->setUrl("http://whatever.com/_{$daysAgo}"); $t->doTrackPageView('First page view'); $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.05)); $t->setUrl("http://whatever.com/42/{$daysAgo}"); $t->doTrackPageView('Second page view'); // track an event to test event actions are purged/preserved $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.1)); $t->setUrl("http://whatever.com/event"); $t->doTrackEvent('Event action', 'event cat', 'daysAgo=' . $daysAgo, 1000); // track a content impression to test that content actions are purged/preserved $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.1)); $t->doTrackContentImpression('SugarTransportAd', '/path/ad.jpg', 'http://www.satsumaprovince.jp'); $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.2)); $t->addEcommerceItem($sku = 'SKU2', $name = 'Canon SLR', $category = 'Electronics & Cameras', $price = 1500, $quantity = 1); $t->doTrackEcommerceOrder($orderId = '937nsjusu ' . $dateTime, $grandTotal = 1111.11, $subTotal = 1000, $tax = 111, $shipping = 0.11, $discount = 666); } Fixture::checkBulkTrackingResponse($t->doBulkTrack()); }