File: SEOTest.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (79 lines) | stat: -rw-r--r-- 2,503 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\SEO\tests\Integration;

use Piwik\DataTable\Renderer;
use Piwik\EventDispatcher;
use Piwik\Piwik;
use Piwik\Plugins\SEO\API;
use Piwik\Plugins\SEO\Metric\Bing;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group SEO
 * @group SEOTest
 * @group Plugins
 */
class SEOTest extends IntegrationTestCase
{
    public function setUp(): void
    {
        parent::setUp();

        // Setup the access layer
        FakeAccess::setIdSitesView([1, 2]);
        FakeAccess::setIdSitesAdmin([3, 4]);

        // Finally we set the user as a Super User by default
        FakeAccess::$superUser = true;

        // Needed to load the Intl_NumberFormatNumber translation string used when formatting the ranking numbers
        Fixture::loadAllTranslations();

        // Bing may not show the indexed pages count for some user agents, This user agent string works as of 2023-06-26:
        $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36';
    }

    /**
     * tell us when the API is broken
     */
    public function testAPI()
    {
        // skip testing Bing metric as it's flaky
        EventDispatcher::getInstance()->addObserver('SEO.getMetricsProviders', function (&$providers) {
            foreach ($providers as $idx => $provider) {
                if ($provider instanceof Bing) {
                    unset($providers[$idx]);
                }
            }
        });

        $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_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'] . ']');
        }
    }

    public function provideContainerConfig()
    {
        return [
          'Piwik\Access' => new FakeAccess(),
        ];
    }
}