File: RoutingTest.php

package info (click to toggle)
phpmyadmin 4%3A5.2.2-really%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 140,312 kB
  • sloc: javascript: 228,447; php: 166,904; xml: 17,847; sql: 504; sh: 275; makefile: 209; python: 205
file content (150 lines) | stat: -rw-r--r-- 4,981 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests;

use FastRoute\DataGenerator\GroupCountBased as DataGeneratorGroupCountBased;
use FastRoute\Dispatcher;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std as RouteParserStd;
use PhpMyAdmin\Controllers\HomeController;
use PhpMyAdmin\Routing;

use function file_exists;
use function file_put_contents;
use function sprintf;
use function unlink;
use function var_export;

use const CACHE_DIR;
use const TEST_PATH;

/**
 * @covers \PhpMyAdmin\Routing
 */
#[\PHPUnit\Framework\Attributes\CoversClass(\PhpMyAdmin\Routing::class)]
class RoutingTest extends AbstractTestCase
{
    public function testGetDispatcherWithDevEnv(): void
    {
        $GLOBALS['cfg']['environment'] = 'development';
        $expected = [Dispatcher::FOUND, HomeController::class, []];
        self::assertSame($expected, Routing::getDispatcher()->dispatch('GET', '/'));
    }

    public function testGetDispatcherWithValidCacheFile(): void
    {
        $GLOBALS['cfg']['environment'] = 'production';
        $_SESSION['isRoutesCacheFileValid'] = true;

        $this->markTestIncomplete('Disabled because the cache folder may not be writable');

        self::assertDirectoryIsWritable(CACHE_DIR);

        $routeCollector = new RouteCollector(new RouteParserStd(), new DataGeneratorGroupCountBased());
        $routeDefinitionCallback = require TEST_PATH . 'libraries/routes.php';
        $routeDefinitionCallback($routeCollector);
        $routesData = sprintf('<?php return %s;', var_export($routeCollector->getData(), true));
        self::assertNotFalse(file_put_contents(Routing::ROUTES_CACHE_FILE, $routesData));

        $expected = [Dispatcher::FOUND, HomeController::class, []];
        self::assertSame($expected, Routing::getDispatcher()->dispatch('GET', '/'));
    }

    public function testGetDispatcherWithInvalidCacheFile(): void
    {
        $GLOBALS['cfg']['environment'] = 'production';
        $_SESSION['isRoutesCacheFileValid'] = null;

        $this->markTestIncomplete('Disabled because the cache folder may not be writable');

        self::assertDirectoryIsWritable(CACHE_DIR);

        $routeCollector = new RouteCollector(new RouteParserStd(), new DataGeneratorGroupCountBased());
        $routeDefinitionCallback = require TEST_PATH . 'libraries/routes.php';
        $routeDefinitionCallback($routeCollector);
        $dispatchData = $routeCollector->getData();
        /** @psalm-suppress MixedArrayAccess */
        unset($dispatchData[0]['GET']['/']);
        $routesData = sprintf('<?php return %s;', var_export($dispatchData, true));
        self::assertNotFalse(file_put_contents(Routing::ROUTES_CACHE_FILE, $routesData));

        $expected = [Dispatcher::FOUND, HomeController::class, []];
        self::assertSame($expected, Routing::getDispatcher()->dispatch('GET', '/'));
    }

    public function testGetDispatcherWithNoCacheFile(): void
    {
        $GLOBALS['cfg']['environment'] = 'production';
        $_SESSION['isRoutesCacheFileValid'] = null;

        $this->markTestIncomplete('Disabled because the cache folder may not be writable');

        self::assertDirectoryIsWritable(CACHE_DIR);
        if (file_exists(Routing::ROUTES_CACHE_FILE)) {
            self::assertTrue(unlink(Routing::ROUTES_CACHE_FILE));
        }

        $expected = [Dispatcher::FOUND, HomeController::class, []];
        self::assertSame($expected, Routing::getDispatcher()->dispatch('GET', '/'));
    }

    /**
     * Test for Routing::getCurrentRoute
     */
    public function testGetCurrentRouteNoParams(): void
    {
        self::assertSame('/', Routing::getCurrentRoute());
    }

    /**
     * Test for Routing::getCurrentRoute
     */
    public function testGetCurrentRouteGet(): void
    {
        $_GET['route'] = '/test';
        self::assertSame('/test', Routing::getCurrentRoute());
    }

    /**
     * Test for Routing::getCurrentRoute
     */
    public function testGetCurrentRoutePost(): void
    {
        unset($_GET['route']);
        $_POST['route'] = '/testpost';
        self::assertSame('/testpost', Routing::getCurrentRoute());
    }

    /**
     * Test for Routing::getCurrentRoute
     */
    public function testGetCurrentRouteGetIsOverPost(): void
    {
        $_GET['route'] = '/testget';
        $_POST['route'] = '/testpost';
        self::assertSame('/testget', Routing::getCurrentRoute());
    }

    /**
     * Test for Routing::getCurrentRoute
     */
    public function testGetCurrentRouteRedirectDbStructure(): void
    {
        unset($_POST['route']);
        unset($_GET['route']);
        $_GET['db'] = 'testDB';
        self::assertSame('/database/structure', Routing::getCurrentRoute());
    }

    /**
     * Test for Routing::getCurrentRoute
     */
    public function testGetCurrentRouteRedirectSql(): void
    {
        $_GET['db'] = 'testDB';
        $_GET['table'] = 'tableTest';
        self::assertSame('/sql', Routing::getCurrentRoute());
    }
}