Passed
Pull Request — master (#303)
by Damian
01:57
created

FluentExtensionTest::testGetLocalisedFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace TractorCow\Fluent\Tests\Extension;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Dev\SapphireTest;
7
use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
8
use TractorCow\Fluent\State\FluentState;
9
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedAnother;
10
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedChild;
11
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedParent;
12
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\UnlocalisedChild;
13
use TractorCow\Fluent\Tests\Extension\Stub\FluentStubObject;
14
15
class FluentExtensionTest extends SapphireTest
16
{
17
    protected static $extra_dataobjects = [
18
        LocalisedAnother::class,
19
        LocalisedChild::class,
20
        LocalisedParent::class,
21
        UnlocalisedChild::class,
22
    ];
23
24
    protected static $required_extensions = [
25
        SiteTree::class => [
26
            FluentSiteTreeExtension::class,
27
        ],
28
    ];
29
30
    public function testFluentLocaleAndFrontendAreAddedToDataQuery()
31
    {
32
        FluentState::singleton()
33
            ->setLocale('test')
34
            ->setIsFrontend(true);
35
36
        $query = SiteTree::get()->dataQuery();
37
        $this->assertSame('test', $query->getQueryParam('Fluent.Locale'));
38
        $this->assertTrue($query->getQueryParam('Fluent.IsFrontend'));
0 ignored issues
show
Bug introduced by
$query->getQueryParam('Fluent.IsFrontend') of type string is incompatible with the type boolean expected by parameter $condition of PHPUnit_Framework_Assert::assertTrue(). ( Ignorable by Annotation )

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

38
        $this->assertTrue(/** @scrutinizer ignore-type */ $query->getQueryParam('Fluent.IsFrontend'));
Loading history...
39
    }
40
41
    public function testGetLocalisedTable()
42
    {
43
        /** @var SiteTree|FluentSiteTreeExtension $page */
44
        $page = new SiteTree;
45
        $this->assertSame('SiteTree_Localised', $page->getLocalisedTable('SiteTree'));
46
        $this->assertSame('SiteTree_Localised_FR', $page->getLocalisedTable('SiteTree', 'FR'));
47
    }
48
49
    public function testGetLinkingMode()
50
    {
51
        // Does not have a canViewInLocale method, locale is not current
52
        $stub = new FluentStubObject();
53
        $this->assertSame('link', $stub->getLinkingMode('foo'));
0 ignored issues
show
Bug introduced by
The method getLinkingMode() does not exist on TractorCow\Fluent\Tests\...n\Stub\FluentStubObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

53
        $this->assertSame('link', $stub->/** @scrutinizer ignore-call */ getLinkingMode('foo'));
Loading history...
54
55
        // Does not have a canViewInLocale method, locale is current
56
        FluentState::singleton()->setLocale('foo');
57
        $this->assertSame('current', $stub->getLinkingMode('foo'));
58
    }
59
60
    public function testGetLocalisedFields()
61
    {
62
        // test data_include / data_exclude
63
        // note: These parent fields should be all accessible from the child records as well
64
        $parent = new LocalisedParent();
65
        $parentLocalised = [
66
            'Title' => 'Varchar',
67
            'Details' => 'Varchar(200)',
68
        ];
69
        $this->assertEquals(
70
            $parentLocalised,
71
            $parent->getLocalisedFields()
0 ignored issues
show
Bug introduced by
The method getLocalisedFields() does not exist on TractorCow\Fluent\Tests\...ionTest\LocalisedParent. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

71
            $parent->/** @scrutinizer ignore-call */ 
72
                     getLocalisedFields()
Loading history...
72
        );
73
74
        // test field_include / field_exclude
75
        $another = new LocalisedAnother();
76
        $this->assertEquals(
77
            [
78
                'Bastion' => 'Varchar',
79
                'Data' => 'Varchar(100)',
80
            ],
81
            $another->getLocalisedFields()
0 ignored issues
show
Bug introduced by
The method getLocalisedFields() does not exist on TractorCow\Fluent\Tests\...onTest\LocalisedAnother. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

81
            $another->/** @scrutinizer ignore-call */ 
82
                      getLocalisedFields()
Loading history...
82
        );
83
        $this->assertEquals(
84
            $parentLocalised,
85
            $another->getLocalisedFields(LocalisedParent::class)
86
        );
87
88
        // Test translate directly
89
        $child = new LocalisedChild();
90
        $this->assertEquals(
91
            [ 'Record' => 'Text' ],
92
            $child->getLocalisedFields()
0 ignored issues
show
Bug introduced by
The method getLocalisedFields() does not exist on TractorCow\Fluent\Tests\...sionTest\LocalisedChild. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

92
            $child->/** @scrutinizer ignore-call */ 
93
                    getLocalisedFields()
Loading history...
93
        );
94
        $this->assertEquals(
95
            $parentLocalised,
96
            $child->getLocalisedFields(LocalisedParent::class)
97
        );
98
99
        // Test 'none'
100
        $unlocalised = new UnlocalisedChild();
101
        $this->assertEmpty($unlocalised->getLocalisedFields());
0 ignored issues
show
Bug introduced by
The method getLocalisedFields() does not exist on TractorCow\Fluent\Tests\...onTest\UnlocalisedChild. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

101
        $this->assertEmpty($unlocalised->/** @scrutinizer ignore-call */ getLocalisedFields());
Loading history...
102
        $this->assertEquals(
103
            $parentLocalised,
104
            $unlocalised->getLocalisedFields(LocalisedParent::class)
105
        );
106
    }
107
}
108