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); }
function checkPiwikSetupForTests() { if (empty($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI'] == '@REQUEST_URI@') { echo "WARNING: for tests to pass, you must first:\n1) Install webserver on localhost, eg. apache\n2) Make these Piwik files available on the webserver, at eg. http://localhost/dev/piwik/\n3) Install Piwik by going through the installation process\n4) Copy phpunit.xml.dist to phpunit.xml\n5) Edit in phpunit.xml the @REQUEST_URI@ and replace with the webserver path to Piwik, eg. '/dev/piwik/'\n\nTry again.\n-> If you still get this message, you can work around it by specifying Host + Request_Uri at the top of this file tests/PHPUnit/bootstrap.php. <-"; exit(1); } $baseUrl = \Piwik\Tests\Framework\Fixture::getRootUrl(); \Piwik\SettingsPiwik::checkPiwikServerWorking($baseUrl, $acceptInvalidSSLCertificates = true); }
public function test_setSegmentsToForceFromSegmentIds_CorrectlyGetsSegmentDefinitions_FromSegmentIds() { Fixture::createWebsite('2014-12-12 00:01:02'); SegmentAPI::getInstance()->add('foo', 'actions>=1', 1, true, true); SegmentAPI::getInstance()->add('barb', 'actions>=2', 1, true, true); SegmentAPI::getInstance()->add('burb', 'actions>=3', 1, true, true); SegmentAPI::getInstance()->add('sub', 'actions>=4', 1, true, true); $cronarchive = new TestCronArchive(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php'); $cronarchive->setSegmentsToForceFromSegmentIds(array(2, 4)); $expectedSegments = array('actions>=2', 'actions>=4'); $this->assertEquals($expectedSegments, array_values($cronarchive->segmentsToForce)); }
/** * @group Core * * @dataProvider getMethodsToTest */ public function testHEADOperation($method) { if ($method == 'fopen') { return; // not supported w/ this method } $result = Http::sendHttpRequestBy($method, Fixture::getRootUrl() . 'tests/PHPUnit/Integration/Http/fixture.zip', 30, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSslCertificate = false, $byteRange = false, $getExtendedInfo = true, $httpMethod = 'HEAD'); $this->assertEquals('', $result['data']); $this->assertEquals(200, $result['status']); $this->assertTrue(isset($result['headers']['Content-Length']), "Content-Length header not set!"); $this->assertTrue(is_numeric($result['headers']['Content-Length']), "Content-Length header not numeric!"); $this->assertEquals('application/zip', $result['headers']['Content-Type']); }
private function getResponseHeader($url) { if (!function_exists('curl_init')) { $this->markTestSkipped('Curl is not installed'); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, Fixture::getRootUrl() . 'tests/PHPUnit/proxy/' . $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); $header = substr($response, 0, $headerSize); curl_close($ch); return $header; }
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); } }
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 sendArchiveWebRequest() { return $this->curl(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/archive.php?token_auth=' . Fixture::getTokenAuth()); }
private function runArchivePhpCron($options = array(), $archivePhpScript = false) { $archivePhpScript = $archivePhpScript ?: PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/archive.php'; $urlToProxy = Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php'; // create the command $cmd = "php \"{$archivePhpScript}\" --url=\"{$urlToProxy}\""; foreach ($options as $name => $value) { $cmd .= " {$name}"; if ($value !== null) { $cmd .= "=" . escapeshellarg($value); } } $cmd .= " 2>&1"; // run the command exec($cmd, $output, $result); if ($result !== 0 || stripos($result, "error")) { $this->fail("archive cron failed: " . implode("\n", $output) . "\n\ncommand used: {$cmd}"); } return $output; }
private function getUrl() { return Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=API&method=API.getPiwikVersion'; }
/** * We override the default values of tests/UI/config.dist.js with config * values from the local INI config. */ private function writeJsConfig() { $localConfigFile = PIWIK_INCLUDE_PATH . '/tests/UI/config.js'; $tag = 'File generated by the tests:run-ui command'; // If the file wasn't generated by this command, we don't ovewrite it if (file_exists($localConfigFile)) { $fileContent = file_get_contents($localConfigFile); if (strpos($fileContent, $tag) === false) { return; } } $url = Fixture::getRootUrl(); $host = Config::getInstance()->tests['http_host']; $uri = Config::getInstance()->tests['request_uri']; $js = <<<JS /** * {$tag} */ exports.piwikUrl = "{$url}"; exports.phpServer = { HTTP_HOST: '{$host}', REQUEST_URI: '{$uri}', REMOTE_ADDR: '127.0.0.1' }; JS; file_put_contents(PIWIK_INCLUDE_PATH . '/tests/UI/config.js', $js); }
/** * @dataProvider getMethodsToTest */ public function testHttpPost_ViaArray($method) { $result = Http::sendHttpRequestBy($method, Fixture::getRootUrl() . 'tests/PHPUnit/Integration/Http/Post.php', 30, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSslCertificate = false, $byteRange = false, $getExtendedInfo = false, $httpMethod = 'POST', $httpUsername = '', $httpPassword = '', array('adf2' => '44', 'afc23' => 'ab12')); $this->assertEquals('{"adf2":"44","afc23":"ab12","method":"post"}', $result); }
private function completeUrl($query) { $host = Fixture::getRootUrl(); if (false === strpos($query, '?')) { $query .= '?'; } else { $query .= '&'; } return $host . 'tests/PHPUnit/proxy/index.php' . $query . 'testmode=1&token_auth=' . $this->authToken; }
private function getOwnPiwikTarget() { return $this->getCustomPiwikTarget(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/piwik.php', $this->idSite); }
private function runArchivePhpCron() { $archivePhpScript = PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/archive.php'; $urlToProxy = Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php'; // create the command $cmd = "php \"{$archivePhpScript}\" --url=\"{$urlToProxy}\" 2>&1"; // run the command exec($cmd, $output, $result); if ($result !== 0 || stripos($result, "error")) { $message = 'This failed once after a lunar eclipse, and it has again randomly failed.'; $message .= "\n\narchive cron failed: " . implode("\n", $output) . "\n\ncommand used: {$cmd}"; $this->markTestSkipped($message); } return $output; }
/** * Helper methods */ private function getStaticSrvUrl() { $url = Fixture::getRootUrl(); $url .= '/tests/resources/'; return $url . "staticFileServer.php?" . FILE_MODE_REQUEST_VAR . "=" . STATIC_SERVER_MODE . "&" . SRV_MODE_REQUEST_VAR . "="; }
/** * Helper methods */ private function getStaticSrvUrl() { return Fixture::getRootUrl(); }
/** * To allow tests to pass no matter what port Piwik is on, we replace the test URL w/ another * one in the response. We don't remove the URL outright, because then we would not be able * to detect regressions where the root URL went missing. * * @param $apiResponse * @return mixed * @throws Exception */ private function replacePiwikUrl($apiResponse) { return str_replace(Fixture::getRootUrl(), "http://example.com/piwik/", $apiResponse); }
/** * @dataProvider getMethodsToTest */ public function testHttpAuthenticationInvalid($method) { $result = Http::sendHttpRequestBy($method, Fixture::getRootUrl() . 'tests/PHPUnit/Integration/Http/HttpAuthentication.php', 30, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSslCertificate = false, $byteRange = false, $getExtendedInfo = true, $httpMethod = 'GET', $httpUsername = '', $httpPassword = ''); $this->assertEquals(401, $result['status']); }