Welcome to mirror list, hosted at ThFree Co, Russian Federation.

APITest.php « System « tests « Installation « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 54a85ec389a037f8269f810bc845787fbe190a68 (plain)
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
80
81
82
83
84
85
86
87
88
89
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\Installation\tests\System;

use Piwik\Http;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\SystemTestCase;

/**
 * @group Installation
 * @group APITest
 * @group Plugins
 */
class APITest extends SystemTestCase
{
    /**
     * @var Fixture
     */
    public static $fixture = null; // initialized below class definition

    public static function setUpBeforeClass(): void
    {
        parent::setUpBeforeClass();

        $testingEnvironment = new \Piwik\Tests\Framework\TestingEnvironmentVariables();
        $testingEnvironment->configFileLocal = PIWIK_INCLUDE_PATH . '/plugins/Installation/tests/resources/config.ini.php';
        $testingEnvironment->save();
    }

    public function test_shouldReturnHttp500_IfWrongDbInfo()
    {
        $response = $this->sendHttpRequest($this->getUrl());
        $this->assertEquals(500, $response['status']);
    }

    public function test_shouldReturnValidApiResponse_IfWrongDbInfo_formatXML()
    {
        $response = $this->sendHttpRequest($this->getUrl());

        $data = str_replace("\n", "", $response['data']);

        $this->assertStringStartsWith('<?xml version="1.0" encoding="utf-8" ?><result>	<error message=', $data);
        self::assertStringContainsString('Database access denied', $data);
        $this->assertStringEndsWith('</result>', $data);
    }

    public function test_shouldReturnValidApiResponse_IfWrongDbInfo_formatJSON()
    {
        $response = $this->sendHttpRequest($this->getUrl() . '&format=json');

        $data = str_replace("\n", "", $response['data']);

        $this->assertStringStartsWith('{"result":"error","message":"', $data);
        self::assertStringContainsString('Database access denied', $data);
    }

    public function test_shouldReturnEmptyResultWhenNotInstalledAndDispatchIsDisabled()
    {
        $url = Fixture::getTestRootUrl() . 'nodispatchnotinstalled.php';
        $response = $this->sendHttpRequest($url);
        $this->assertSame('', $response['data']);
    }

    private function getUrl()
    {
        return Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=API&method=API.getPiwikVersion';
    }

    public static function getOutputPrefix()
    {
        return '';
    }

    public static function getPathToTestDirectory()
    {
        return dirname(__FILE__);
    }

    protected function sendHttpRequest($url)
    {
        return Http::sendHttpRequest($url, 10, null, null, 0, false, false, true);
    }
}