File size: 4,756 Bytes
bf8c0ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?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\Tests\Integration;

use Piwik\Access;
use Piwik\Auth;
use Piwik\Container\StaticContainer;
use Piwik\FrontController;
use Piwik\Http;
use Piwik\Session;
use Piwik\Session\SessionFingerprint;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

class FrontControllerTest extends IntegrationTestCase
{
    public function testFatalErrorStackTracesReturned()
    {
        $url = Fixture::getRootUrl() . '/tests/resources/trigger-fatal.php?format=json';
        $response = Http::sendHttpRequest($url, self::isCIEnvironment() ? 5 : 20);

        $response = json_decode($response, $isAssoc = true);
        $response['message'] = $this->cleanMessage($response['message']);

        $this->assertEquals('error', $response['result']);

        $expectedFormat = <<<FORMAT
Allowed memory size of %s bytes exhausted (tried to allocate %s bytes) on /tests/resources/trigger-fatal.php(23) #0 /tests/resources/trigger-fatal.php(36): MyClass-&gt;triggerError(arg1=&quot;argval&quot;, arg2=&quot;another&quot;) #1 /tests/resources/trigger-fatal.php(52): MyDerivedClass::staticMethod() #2 /tests/resources/trigger-fatal.php(58): myFunction()
FORMAT;

        $this->assertStringMatchesFormat($expectedFormat, $response['message']);
    }

    public function testThrownExceptionInFrontControllerPrintsBacktrace()
    {
        $url = Fixture::getRootUrl() . '/tests/resources/trigger-fatal-exception.php?format=json';
        $response = Http::sendHttpRequest($url, self::isCIEnvironment() ? 5 : 20);

        $response = json_decode($response, $isAssoc = true);
        $response['message'] = $this->cleanMessage($response['message']);

        $this->assertEquals('error', $response['result']);

        $expectedFormat = <<<FORMAT
test message on /tests/resources/trigger-fatal-exception.php(23) #0 [internal function]: {closure}('CoreHome', 'index', Array) #1 /core/EventDispatcher.php(141): call_user_func_array(Object(Closure), Array) #2 /core/Piwik.php(845): Piwik\EventDispatcher-&gt;postEvent('Request.dispatc...', Array, false, Array) #3 /core/FrontController.php(606): Piwik\Piwik::postEvent('Request.dispatc...', Array) #4 /core/FrontController.php(168): Piwik\FrontController-&gt;doDispatch('CoreHome', 'index', Array) #5 /tests/resources/trigger-fatal-exception.php(31): Piwik\FrontController-&gt;dispatch('CoreHome', 'index') #6 {main}
FORMAT;

        if (version_compare(PHP_VERSION, '8.4.0-dev', '>=')) {
            $expectedFormat = <<<FORMAT
test message on /tests/resources/trigger-fatal-exception.php(23) #0 [internal function]: {closure:/tests/resources/trigger-fatal-exception.php:20}('...', '...', Array) #1 /core/EventDispatcher.php(147): call_user_func_array(Object(Closure), Array) #2 /core/Piwik.php(880): Piwik\EventDispatcher-&gt;postEvent('...', Array, false, Array) #3 /core/FrontController.php(625): Piwik\Piwik::postEvent('...', Array) #4 /core/FrontController.php(169): Piwik\FrontController-&gt;doDispatch('...', '...', Array) #5 /tests/resources/trigger-fatal-exception.php(31): Piwik\FrontController-&gt;dispatch('...', '...') #6 {main}
FORMAT;
        }

        //remove all the numbers
        $expectedFormat = preg_replace('/[0-9]+/', 'x', $expectedFormat);
        $expectedFormat = preg_replace('/".*?"|\'.*?\'/', 'xxx', $expectedFormat);

        $actualFormat = preg_replace('/[0-9]+/', 'x', $response['message']);
        $actualFormat = preg_replace('/".*?"|\'.*?\'/', 'xxx', $actualFormat);

        $this->assertStringMatchesFormat($expectedFormat, $actualFormat);
    }

    /**
     * @runInSeparateProcess
     */
    public function testAuthImplementationConfiguredEvenIfSessionAuthSucceeds()
    {
        Session::start();

        Access::getInstance()->setSuperUserAccess(false);

        $sessionFingerprint = new SessionFingerprint();
        $sessionFingerprint->initialize('superUserLogin', Fixture::getTokenAuth());

        FrontController::getInstance()->init();

        /** @var \Piwik\Plugins\Login\Auth $auth */
        $auth = StaticContainer::get(Auth::class);
        $this->assertInstanceOf(\Piwik\Plugins\Login\Auth::class, $auth);

        $this->assertEquals('superUserLogin', $auth->getLogin());
        $this->assertEquals(Fixture::getTokenAuth(), $auth->getTokenAuth());
    }

    private function cleanMessage($message)
    {
        return trim($message);
    }

    /**
     * @param Fixture $fixture
     */
    protected static function configureFixture($fixture)
    {
        parent::configureFixture($fixture);
        $fixture->createSuperUser = true;
    }
}