diff options
author | Peter Zhang <peter@innocraft.com> | 2021-10-14 06:42:53 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-14 06:42:53 +0300 |
commit | 39fb369f8107d24c96480bfdeea8d9bf2237080a (patch) | |
tree | e5801b597d9f6fc71a6e0928ffc1a470f4af0933 /plugins/SEO | |
parent | 4764df9b0c795301976788df87fe314ecc7acece (diff) |
update SEO Test (#18142)
Diffstat (limited to 'plugins/SEO')
-rw-r--r-- | plugins/SEO/Metric/Alexa.php | 6 | ||||
-rw-r--r-- | plugins/SEO/Metric/Bing.php | 9 | ||||
-rw-r--r-- | plugins/SEO/Metric/Google.php | 24 | ||||
-rw-r--r-- | plugins/SEO/tests/Integration/SEOTest.php | 35 |
4 files changed, 31 insertions, 43 deletions
diff --git a/plugins/SEO/Metric/Alexa.php b/plugins/SEO/Metric/Alexa.php index a9a58160e0..c0ae41854a 100644 --- a/plugins/SEO/Metric/Alexa.php +++ b/plugins/SEO/Metric/Alexa.php @@ -34,6 +34,7 @@ class Alexa implements MetricsProvider public function getMetrics($domain) { $value = null; + $suffix = 'General_Pages'; try { $response = Http::sendHttpRequest(self::URL . urlencode($domain ?? ''), $timeout = 10, @$_SERVER['HTTP_USER_AGENT']); libxml_use_internal_errors(true); // suppress errors @@ -50,14 +51,15 @@ class Alexa implements MetricsProvider } } catch (\Exception $e) { $this->logger->info('Error while getting Alexa SEO stats: {message}', array('message' => $e->getMessage())); - $value = Piwik::translate('General_Error'); + $value = Piwik::translate('General_ErrorTryAgain'); + $suffix = ''; } $logo = "plugins/Morpheus/icons/dist/SEO/alexa.com.png"; $link = self::LINK . urlencode($domain ?? ''); return array( - new Metric('alexa', 'SEO_AlexaRank', $value, $logo, $link) + new Metric('alexa', 'SEO_AlexaRank', $value, $logo, $link, null, $suffix) ); } } diff --git a/plugins/SEO/Metric/Bing.php b/plugins/SEO/Metric/Bing.php index 1279b77894..929ea4e0c3 100644 --- a/plugins/SEO/Metric/Bing.php +++ b/plugins/SEO/Metric/Bing.php @@ -33,25 +33,28 @@ class Bing implements MetricsProvider public function getMetrics($domain) { $url = self::URL . urlencode($domain ?? ''); - + $suffix = ''; try { $response = str_replace(' ', ' ', Http::sendHttpRequest($url, $timeout = 10, @$_SERVER['HTTP_USER_AGENT'])); $response = str_replace(' ', '', $response); // number uses nbsp as thousand separator if (preg_match('#([0-9,\.]+) results#i', $response, $p)) { $pageCount = NumberFormatter::getInstance()->formatNumber((int)str_replace(array(',', '.'), '', $p[1])); + $suffix = 'General_Pages'; + } elseif (preg_match('#There are no results#i', $response, $p)) { + $pageCount = Piwik::translate('General_ErrorTryAgain'); } else { $pageCount = 0; } } catch (\Exception $e) { $this->logger->info('Error while getting Bing SEO stats: {message}', array('message' => $e->getMessage())); - $pageCount = Piwik::translate('General_Error'); + $pageCount = Piwik::translate('General_ErrorTryAgain'); } $logo = "plugins/Morpheus/icons/dist/SEO/bing.com.png"; return array( - new Metric('bing-index', 'SEO_Bing_IndexedPages', $pageCount, $logo, null, null, 'General_Pages') + new Metric('bing-index', 'SEO_Bing_IndexedPages', $pageCount, $logo, null, null, $suffix) ); } } diff --git a/plugins/SEO/Metric/Google.php b/plugins/SEO/Metric/Google.php index 14f6e3d9f2..362bdfd207 100644 --- a/plugins/SEO/Metric/Google.php +++ b/plugins/SEO/Metric/Google.php @@ -35,31 +35,31 @@ class Google implements MetricsProvider public function getMetrics($domain) { - $pageCount = $this->fetchIndexedPagesCount($domain); $logo = "plugins/Morpheus/icons/dist/SEO/google.com.png"; - return array( - new Metric('google-index', 'SEO_Google_IndexedPages', $pageCount, $logo, null, null, 'General_Pages'), - ); - } - - public function fetchIndexedPagesCount($domain) - { $url = self::SEARCH_URL . urlencode($domain ?? ''); - + $suffix = ''; try { $response = str_replace(' ', ' ', Http::sendHttpRequest($url, $timeout = 10, @$_SERVER['HTTP_USER_AGENT'])); if (preg_match('#([0-9,\.]+) results#i', $response, $p)) { - return NumberFormatter::getInstance()->formatNumber((int)str_replace(array(',', '.'), '', $p[1])); + $pageCount = NumberFormatter::getInstance()->formatNumber((int)str_replace(array(',', '.'), '', $p[1])); + $suffix = 'General_Pages'; + } elseif (preg_match('#did not match any#i', $response, $p)) { + $pageCount = Piwik::translate('General_ErrorTryAgain'); } else { - return 0; + $pageCount = 0; } } catch (\Exception $e) { $this->logger->info('Error while getting Google search SEO stats: {message}', array('message' => $e->getMessage())); - return Piwik::translate('General_Error'); + return Piwik::translate('General_ErrorTryAgain'); } + + return array( + new Metric('google-index', 'SEO_Google_IndexedPages', $pageCount, $logo, null, null, $suffix), + ); } + } diff --git a/plugins/SEO/tests/Integration/SEOTest.php b/plugins/SEO/tests/Integration/SEOTest.php index 4d3cb1e577..67c99f9356 100644 --- a/plugins/SEO/tests/Integration/SEOTest.php +++ b/plugins/SEO/tests/Integration/SEOTest.php @@ -9,6 +9,8 @@ namespace Piwik\Plugins\SEO\tests\Integration; use Piwik\DataTable\Renderer; +use Piwik\Http; +use Piwik\NumberFormatter; use Piwik\Piwik; use Piwik\Plugins\SEO\API; use Exception; @@ -41,43 +43,24 @@ class SEOTest extends IntegrationTestCase */ public function test_API() { - $ip = $_SERVER['REMOTE_ADDR'] ?? ''; - $ranks = $this->apiFunction(); + $dataTable = API::getInstance()->getRank('http://matomo.org/'); + $renderer = Renderer::factory('json'); + $renderer->setTable($dataTable); + $ranks = json_decode($renderer->render(), true); foreach ($ranks as $rank) { - if ($rank['rank'] == Piwik::translate('General_Error')) { + if ($rank['rank'] == Piwik::translate('General_ErrorTryAgain')) { $this->markTestSkipped('An exception raised when fetching data. Skipping this test for now.'); continue; } - $this->assertNotEmpty($rank['rank'], - $rank['id'] . ' expected non-zero rank, got [' . $rank['rank'] . '], ip [' .$ip . ']'); + $this->assertNotEmpty($rank['rank'], $rank['id'] . ' expected non-zero rank, got [' . $rank['rank'] . ']'); } } public function provideContainerConfig() { return array( - 'Piwik\Access' => new FakeAccess() + 'Piwik\Access' => new FakeAccess() ); } - /** - * this function rerun the API 3 times, to reduce the chance of failing. - */ - private function apiFunction($counter = 0, $ranks = []) - { - if ($counter > 3) { - return $ranks; - } - $dataTable = API::getInstance()->getRank('http://matomo.org/'); - $renderer = Renderer::factory('json'); - $renderer->setTable($dataTable); - $ranks = json_decode($renderer->render(), true); - foreach ($ranks as $rank) { - if ($rank['rank'] === 0 ) { - $this->apiFunction($counter++, $ranks); - } - } - return $ranks; - } - } |