Completed
Push — EZP-31044-site-access-provider ( 8220ce )
by
unknown
14:19
created

RouterURIElement2Test::createRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 22
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\RouterURIElement2Test class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests;
10
11
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
12
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement;
13
use PHPUnit\Framework\TestCase;
14
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Router;
15
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement as URIElementMatcher;
16
use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest;
17
use eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder;
18
use Psr\Log\LoggerInterface;
19
20
class RouterURIElement2Test extends TestCase
21
{
22
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder */
23
    private $matcherBuilder;
24
25
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface */
26
    private $siteAccessProvider;
27
28
    protected function setUp(): void
29
    {
30
        parent::setUp();
31
        $this->matcherBuilder = new MatcherBuilder();
32
        $this->siteAccessProvider = $this->createMock(SiteAccess\SiteAccessProviderInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...oviderInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\M...ccessProviderInterface> of property $siteAccessProvider.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
        $this->siteAccessProvider
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
            ->method('isDefined')
35
            ->willReturnMap([
36
                ['first_sa', true],
37
                ['second_sa', true],
38
                ['third_sa', true],
39
                ['fourth_sa', true],
40
                ['fifth_sa', true],
41
                ['test_foo', true],
42
                ['first_sa_foo', true],
43
                ['second_sa_foo', true],
44
                ['foo_baz', true],
45
            ]);
46
        $this->siteAccessProvider
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
            ->method('getSiteAccess')
48
            ->willReturnMap([
49
                ['first_sa', new SiteAccess('first_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
50
                ['second_sa', new SiteAccess('second_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
51
                ['third_sa', new SiteAccess('third_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
52
                ['fourth_sa', new SiteAccess('fourth_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
53
                ['fifth_sa', new SiteAccess('fifth_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
54
                ['test_foo', new SiteAccess('test_foo', Router::DEFAULT_SA_MATCHING_TYPE)],
55
                ['first_sa_foo', new SiteAccess('first_sa_foo', Router::DEFAULT_SA_MATCHING_TYPE)],
56
                ['second_sa_foo', new SiteAccess('second_sa_foo', Router::DEFAULT_SA_MATCHING_TYPE)],
57
                ['foo_baz', new SiteAccess('foo_baz', Router::DEFAULT_SA_MATCHING_TYPE)],
58
            ]);
59
    }
60
61
    public function testConstruct()
62
    {
63
        return $this->createRouter();
64
    }
65
66
    /**
67
     * @dataProvider matchProvider
68
     */
69 View Code Duplication
    public function testMatch(SimplifiedRequest $request, string $siteAccess)
70
    {
71
        $router = $this->createRouter();
72
        $sa = $router->match($request);
73
        $this->assertInstanceOf(SiteAccess::class, $sa);
74
        $this->assertSame($siteAccess, $sa->name);
75
        $router->setSiteAccess();
76
    }
77
78 View Code Duplication
    public function matchProvider()
79
    {
80
        return [
81
            [SimplifiedRequest::fromUrl('http://example.com'), 'default_sa'],
82
            [SimplifiedRequest::fromUrl('https://example.com'), 'default_sa'],
83
            [SimplifiedRequest::fromUrl('http://example.com/'), 'default_sa'],
84
            [SimplifiedRequest::fromUrl('https://example.com/'), 'default_sa'],
85
            [SimplifiedRequest::fromUrl('http://example.com//'), 'default_sa'],
86
            [SimplifiedRequest::fromUrl('https://example.com//'), 'default_sa'],
87
            [SimplifiedRequest::fromUrl('http://example.com:8080/'), 'default_sa'],
88
            [SimplifiedRequest::fromUrl('http://example.com/first_siteaccess/'), 'default_sa'],
89
            [SimplifiedRequest::fromUrl('http://example.com/?first_siteaccess'), 'default_sa'],
90
            [SimplifiedRequest::fromUrl('http://example.com/?first_sa'), 'default_sa'],
91
            [SimplifiedRequest::fromUrl('http://example.com/first_salt'), 'default_sa'],
92
            [SimplifiedRequest::fromUrl('http://example.com/first_sa.foo'), 'default_sa'],
93
            [SimplifiedRequest::fromUrl('http://example.com/test'), 'default_sa'],
94
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/'), 'test_foo'],
95
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/'), 'test_foo'],
96
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/first_sa'), 'test_foo'],
97
            [SimplifiedRequest::fromUrl('http://example.com/default_sa'), 'default_sa'],
98
99
            [SimplifiedRequest::fromUrl('http://example.com/first_sa'), 'first_sa'],
100
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/'), 'first_sa'],
101
            // Double slashes shouldn't be considered as one
102
            [SimplifiedRequest::fromUrl('http://example.com//first_sa//'), 'default_sa'],
103
            [SimplifiedRequest::fromUrl('http://example.com///first_sa///test'), 'default_sa'],
104
            [SimplifiedRequest::fromUrl('http://example.com//first_sa//foo/bar'), 'default_sa'],
105
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/foo//bar/'), 'first_sa'],
106
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/foo'), 'first_sa_foo'],
107
            [SimplifiedRequest::fromUrl('http://example.com:82/first_sa/'), 'first_sa'],
108
            [SimplifiedRequest::fromUrl('http://third_siteaccess/first_sa/'), 'first_sa'],
109
            [SimplifiedRequest::fromUrl('http://first_sa/'), 'first_sa'],
110
            [SimplifiedRequest::fromUrl('https://first_sa/'), 'first_sa'],
111
            [SimplifiedRequest::fromUrl('http://first_sa:81/'), 'first_sa'],
112
            [SimplifiedRequest::fromUrl('http://first_siteaccess/'), 'first_sa'],
113
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/'), 'first_sa'],
114
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/'), 'first_sa'],
115
            [SimplifiedRequest::fromUrl('http://first_siteaccess/foo/'), 'first_sa'],
116
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/foo/baz/'), 'foo_baz'],
117
118
            [SimplifiedRequest::fromUrl('http://example.com/second_sa'), 'second_sa'],
119
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/'), 'second_sa'],
120
            [SimplifiedRequest::fromUrl('http://example.com/second_sa?param1=foo'), 'second_sa'],
121
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/foo/'), 'second_sa_foo'],
122
            [SimplifiedRequest::fromUrl('http://example.com:82/second_sa/'), 'second_sa'],
123
            [SimplifiedRequest::fromUrl('http://example.com:83/second_sa/'), 'second_sa'],
124
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/second_sa/'), 'second_sa'],
125
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/second_sa/'), 'second_sa'],
126
        ];
127
    }
128
129
    /**
130
     * @param int $level
131
     * @param string $uri
132
     * @param string $expectedFixedUpURI
133
     *
134
     * @dataProvider analyseProvider
135
     */
136 View Code Duplication
    public function testAnalyseURI($level, $uri, $expectedFixedUpURI)
137
    {
138
        $matcher = new URIElementMatcher([$level]);
139
        $matcher->setRequest(
140
            new SimplifiedRequest(['pathinfo' => $uri])
141
        );
142
        $this->assertSame($expectedFixedUpURI, $matcher->analyseURI($uri));
143
    }
144
145
    /**
146
     * @param int $level
147
     * @param string $uri
148
     * @param string $expectedFixedUpURI
149
     *
150
     * @dataProvider analyseProvider
151
     */
152 View Code Duplication
    public function testAnalyseURILevelAsInt($level, $uri, $expectedFixedUpURI)
153
    {
154
        $matcher = new URIElementMatcher($level);
155
        $matcher->setRequest(
156
            new SimplifiedRequest(['pathinfo' => $uri])
157
        );
158
        $this->assertSame($expectedFixedUpURI, $matcher->analyseURI($uri));
159
    }
160
161
    /**
162
     * @param int $level
163
     * @param string $fullUri
164
     * @param string $linkUri
165
     *
166
     * @dataProvider analyseProvider
167
     */
168 View Code Duplication
    public function testAnalyseLink($level, $fullUri, $linkUri)
169
    {
170
        $matcher = new URIElementMatcher([$level]);
171
        $matcher->setRequest(
172
            new SimplifiedRequest(['pathinfo' => $fullUri])
173
        );
174
        $this->assertSame($fullUri, $matcher->analyseLink($linkUri));
175
    }
176
177
    public function analyseProvider()
178
    {
179
        return [
180
            [2, '/my/siteaccess/foo/bar', '/foo/bar'],
181
            [2, '/vive/le/sucre/en-poudre', '/sucre/en-poudre'],
182
            // Issue https://jira.ez.no/browse/EZP-20125
183
            [1, '/fre/content/edit/104/1/fre-FR', '/content/edit/104/1/fre-FR'],
184
            [1, '/fre/utf8-with-accent/é/fre/à/à/fre/é', '/utf8-with-accent/é/fre/à/à/fre/é'],
185
            [2, '/é/fre/utf8-with-accent/é/fre/à/à/fre/é', '/utf8-with-accent/é/fre/à/à/fre/é'],
186
            [2, '/prefix/fre/url/alias/prefix/fre/prefix/fre/url', '/url/alias/prefix/fre/prefix/fre/url'],
187
            // regression after the first fix of EZP-20125
188
            [1, '/sitaccess', ''],
189
            [1, '/sitaccess/', '/'],
190
            [2, '/prefix/siteaccess', ''],
191
            [2, '/prefix/siteaccess/', '/'],
192
        ];
193
    }
194
195
    /**
196
     * @dataProvider reverseMatchProvider
197
     */
198
    public function testReverseMatch($siteAccessName, $originalPathinfo)
199
    {
200
        $expectedSiteAccessPath = str_replace('_', '/', $siteAccessName);
201
        $matcher = new URIElementMatcher([2]);
202
        $matcher->setRequest(new SimplifiedRequest(['pathinfo' => $originalPathinfo]));
203
204
        $result = $matcher->reverseMatch($siteAccessName);
205
        $this->assertInstanceOf(URIElement::class, $result);
206
        $this->assertSame("/{$expectedSiteAccessPath}{$originalPathinfo}", $result->getRequest()->pathinfo);
207
        $this->assertSame("/$expectedSiteAccessPath/some/linked/uri", $result->analyseLink('/some/linked/uri'));
208
        $this->assertSame('/foo/bar/baz', $result->analyseURI("/$expectedSiteAccessPath/foo/bar/baz"));
209
    }
210
211
    public function reverseMatchProvider()
212
    {
213
        return [
214
            ['some_thing', '/foo/bar'],
215
            ['another_siteaccess', '/foo/bar'],
216
        ];
217
    }
218
219
    public function testReverseMatchFail()
220
    {
221
        $matcher = new URIElementMatcher([2]);
222
        $matcher->setRequest(new SimplifiedRequest(['pathinfo' => '/my/siteaccess/foo/bar']));
223
        $this->assertNull($matcher->reverseMatch('another_siteaccess_again_dont_tell_me'));
224
    }
225
226 View Code Duplication
    public function testSerialize()
227
    {
228
        $matcher = new URIElementMatcher([2]);
229
        $matcher->setRequest(new SimplifiedRequest(['pathinfo' => '/foo/bar']));
230
        $sa = new SiteAccess('test', 'test', $matcher);
231
        $serializedSA1 = serialize($sa);
232
233
        $matcher->setRequest(new SimplifiedRequest(['pathinfo' => '/foo/bar/baz']));
234
        $serializedSA2 = serialize($sa);
235
236
        $this->assertSame($serializedSA1, $serializedSA2);
237
    }
238
239 View Code Duplication
    private function createRouter(): Router
240
    {
241
        return new Router(
242
            $this->matcherBuilder,
243
            $this->createMock(LoggerInterface::class),
244
            'default_sa',
245
            [
246
                'URIElement' => [
247
                    'value' => 2,
248
                ],
249
                'Map\\URI' => [
250
                    'first_sa' => 'first_sa',
251
                    'second_sa' => 'second_sa',
252
                ],
253
                'Map\\Host' => [
254
                    'first_sa' => 'first_sa',
255
                    'first_siteaccess' => 'first_sa',
256
                ],
257
            ],
258
            $this->siteAccessProvider
259
        );
260
    }
261
}
262