FluentBadgeExtensionTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
c 2
b 0
f 0
dl 0
loc 68
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 1
A testDefaultLocaleBadgeAdded() 0 12 1
A testInvisibleLocaleBadgeWasAdded() 0 11 1
1
<?php
2
3
namespace TractorCow\Fluent\Tests\Extension;
4
5
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree 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\Control\Controller;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\ORM\FieldType\DBHTMLText;
9
use TractorCow\Fluent\Extension\FluentLeftAndMainExtension;
10
use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
11
use TractorCow\Fluent\Model\Locale;
12
use TractorCow\Fluent\State\FluentState;
13
use TractorCow\Fluent\Tests\Extension\Stub\FluentStubController;
14
15
class FluentBadgeExtensionTest extends SapphireTest
16
{
17
    protected static $fixture_file = 'FluentBadgeExtensionTest.yml';
18
19
    protected static $required_extensions = [
20
        SiteTree::class => [
21
            FluentSiteTreeExtension::class,
22
        ],
23
    ];
24
25
    /**
26
     * @var SiteTree
27
     */
28
    protected $mockPage;
29
30
    /**
31
     * @var Controller
32
     */
33
    protected $mockController;
34
35
    /**
36
     * @var FluentLeftAndMainExtension
37
     */
38
    protected $extension;
39
40
    protected function setUp(): void
41
    {
42
        parent::setUp();
43
44
        // Clear cache
45
        Locale::clearCached();
46
47
        FluentState::singleton()->withState(function (FluentState $newState) {
48
            $newState->setLocale('en_NZ');
49
50
            $this->mockPage = $this->objFromFixture(SiteTree::class, 'test_page');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objFromFixture(Si...ee::class, 'test_page') of type SilverStripe\ORM\DataObject is incompatible with the declared type SilverStripe\CMS\Model\SiteTree of property $mockPage.

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...
51
            $this->mockController = new FluentStubController($this->mockPage->ID);
52
            $this->extension = new FluentLeftAndMainExtension();
53
            $this->extension->setOwner($this->mockController);
54
        });
55
    }
56
57
    public function testDefaultLocaleBadgeAdded()
58
    {
59
        // Publish the page in the default locale
60
        FluentState::singleton()->withState(function (FluentState $newState) {
61
            $newState->setLocale('en_NZ');
62
            $this->mockPage->publishRecursive();
63
64
            $result = $this->extension->getBadge($this->mockPage);
65
            $this->assertInstanceOf(DBHTMLText::class, $result);
66
            $this->assertStringContainsString('fluent-badge--default', $result->getValue());
67
            $this->assertStringContainsString('Localised in', $result->getValue());
68
            $this->assertStringContainsString('NZ', $result->getValue(), 'Badge shows owner locale');
69
        });
70
    }
71
72
    public function testInvisibleLocaleBadgeWasAdded()
73
    {
74
        FluentState::singleton()->withState(function (FluentState $newState) {
75
            // Don't write the page in the non-default locale, then it shouldn't exist
76
            $newState->setLocale('de_DE');
77
78
            $result = $this->extension->getBadge($this->mockPage);
79
            $this->assertInstanceOf(DBHTMLText::class, $result);
80
            $this->assertStringContainsString('fluent-badge--invisible', $result->getValue());
81
            $this->assertStringContainsString('Page has no available content in', $result->getValue());
82
            $this->assertStringContainsString('de_DE', $result->getValue(), 'Badge shows owner locale');
83
        });
84
    }
85
}
86