Passed
Push — master ( ba460f...5be854 )
by Robbie
01:56 queued 10s
created

testLocalizedControllerRouting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 18
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace TractorCow\Fluent\Tests\Extension;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Dev\FunctionalTest;
11
use TractorCow\Fluent\Extension\FluentDirectorExtension;
12
use TractorCow\Fluent\Extension\FluentFilteredExtension;
13
use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
14
use TractorCow\Fluent\Model\Locale;
15
use TractorCow\Fluent\State\FluentState;
16
use TractorCow\Fluent\Tests\Extension\FluentDirectorExtensionTest\TestController;
17
18
/**
19
 * Class FluentDirectorExtensionTest
20
 *
21
 * @package TractorCow\Fluent\Tests\Extension
22
 */
23
class FluentDirectorExtensionTest extends FunctionalTest
24
{
25
    /**
26
     * @var string
27
     */
28
    protected static $fixture_file = 'FluentDirectorExtensionTest.yml';
29
30
    /**
31
     * @var array
32
     */
33
    protected static $required_extensions = [
34
        SiteTree::class => [
35
            FluentSiteTreeExtension::class,
36
            FluentFilteredExtension::class,
37
        ],
38
        Director::class => [
39
            FluentDirectorExtension::class,
40
        ],
41
    ];
42
43
    public function setUp() // phpcs:ignore SlevomatCodingStandard.TypeHints
44
    {
45
        parent::setUp();
46
47
        $this->logInWithPermission('ADMIN');
48
    }
49
50
    public function testVisitUrlByLocaleWithMultiByteCharacter()
51
    {
52
        $locale = 'en_NZ';
53
        FluentState::singleton()->withState(function (FluentState $state) use ($locale) {
54
            $state->setLocale($locale);
55
56
            $expectedTitle = sprintf('Page1 (%s)', $locale);
57
58
            /** @var Page|FluentSiteTreeExtension $page */
59
            $page = Page::get()->filter(['Title' => $expectedTitle])->first();
60
            $this->assertNotEmpty($page);
61
            $page->publishRecursive();
0 ignored issues
show
Bug introduced by
The method publishRecursive() does not exist on TractorCow\Fluent\Extens...FluentSiteTreeExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            $page->/** @scrutinizer ignore-call */ 
62
                   publishRecursive();

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...
62
63
            $this->get($page->AbsoluteLink());
0 ignored issues
show
Bug introduced by
The method AbsoluteLink() does not exist on TractorCow\Fluent\Extens...FluentSiteTreeExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
            $this->get($page->/** @scrutinizer ignore-call */ AbsoluteLink());

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...
64
65
            $this->assertContains(sprintf('<title>%s', $expectedTitle), $this->content());
66
        });
67
    }
68
69
    public function testLocalizedControllerRouting()
70
    {
71
        $locale = 'en_NZ';
72
        FluentState::singleton()->withState(function (FluentState $state) use ($locale) {
73
            $state->setLocale($locale);
74
75
            $expectedTitle = sprintf('Page1 (%s)', $locale);
76
77
            /** @var Page|FluentSiteTreeExtension $page */
78
            $page = Page::get()->filter(['Title' => $expectedTitle])->first();
79
            $this->assertNotEmpty($page);
80
            $page->publishRecursive();
81
        });
82
83
84
        $this->get(Director::absoluteURL('nouvelle-z%C3%A9lande/TestController'));
85
86
        $this->assertContains('Test Controller! en_NZ', $this->content());
87
    }
88
89
    protected function setUpRoutes()
90
    {
91
        parent::setUpRoutes();
92
93
        // Add controller-name auto-routing
94
        $rules = Director::config()->rules;
95
96
        // Modify the rule for our test controller to include the locale parameter
97
        $i = array_search('admin', array_keys($rules));
98
        if ($i !== false) {
99
            $rule = [
100
                'nouvelle-z%C3%A9lande/TestController//$Action/$ID/$OtherID' => [
101
                    'Controller' => TestController::class,
102
                    'l' => 'en_NZ'
103
                ]
104
            ];
105
106
            $rules = array_slice($rules, 0, $i, true) + $rule + array_slice($rules, $i, null, true);
0 ignored issues
show
Bug introduced by
It seems like $i can also be of type string; however, parameter $length of array_slice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
            $rules = array_slice($rules, 0, /** @scrutinizer ignore-type */ $i, true) + $rule + array_slice($rules, $i, null, true);
Loading history...
Bug introduced by
It seems like $i can also be of type string; however, parameter $offset of array_slice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
            $rules = array_slice($rules, 0, $i, true) + $rule + array_slice($rules, /** @scrutinizer ignore-type */ $i, null, true);
Loading history...
107
        } else {
108
            throw new \Exception('Could not find "admin" url rule');
109
        }
110
111
        // Add controller-name auto-routing
112
        Director::config()->set('rules', $rules);
113
    }
114
}
115