Completed
Pull Request — master (#264)
by Robbie
13:29
created

TranslatableSiteConfigTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\Translatable\Tests;
4
5
use Page;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Security\Member;
9
use SilverStripe\SiteConfig\SiteConfig;
10
use SilverStripe\Translatable\Model\Translatable;
11
12
/**
13
 * @package translatable
14
 */
15
class TranslatableSiteConfigTest extends SapphireTest
16
{
17
    protected static $fixture_file = 'TranslatableSiteConfigTest.yml';
18
19
    protected static $required_extensions = [
20
        SiteTree::class => [Translatable::class],
21
        SiteConfig::class => [Translatable::class],
22
    ];
23
24
    protected static $illegal_extensions = [
25
        // @todo: Update namespace to match subsites
26
        SiteTree::class => array('SiteTreeSubsites')
27
    ];
28
29
    private $origLocale;
30
31
    protected function setUp()
32
    {
33
        parent::setUp();
34
35
        $this->origLocale = Translatable::default_locale();
36
        Translatable::set_default_locale('en_US');
37
    }
38
39
    protected function tearDown()
40
    {
41
        Translatable::set_default_locale($this->origLocale);
42
        Translatable::set_current_locale($this->origLocale);
43
44
        parent::tearDown();
45
    }
46
47
    public function testCurrentCreatesDefaultForLocale()
48
    {
49
        Translatable::set_current_locale(Translatable::default_locale());
50
        $configEn = SiteConfig::current_site_config();
51
        Translatable::set_current_locale('fr_FR');
52
        $configFr = SiteConfig::current_site_config();
53
        Translatable::set_current_locale(Translatable::default_locale());
54
55
        $this->assertInstanceOf(SiteConfig::class, $configFr);
56
        $this->assertEquals($configFr->Locale, 'fr_FR');
57
        $this->assertEquals($configFr->Title, $configEn->Title, 'Copies title from existing config');
58
        $this->assertEquals(
59
            $configFr->getTranslationGroup(),
60
            $configEn->getTranslationGroup(),
61
            'Created in the same translation group'
62
        );
63
    }
64
65
    public function testCanEditTranslatedRootPages()
66
    {
67
        $configEn = $this->objFromFixture(SiteConfig::class, 'en_US');
0 ignored issues
show
Unused Code introduced by
$configEn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
        $configDe = $this->objFromFixture(SiteConfig::class, 'de_DE');
0 ignored issues
show
Unused Code introduced by
$configDe is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
70
        $pageEn = $this->objFromFixture(Page::class, 'root_en');
71
        $pageDe = $pageEn->createTranslation('de_DE');
0 ignored issues
show
Unused Code introduced by
$pageDe is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
73
        $translatorDe = $this->objFromFixture(Member::class, 'translator_de');
74
        $translatorEn = $this->objFromFixture(Member::class, 'translator_en');
75
76
        $this->assertFalse($pageEn->canEdit($translatorDe));
0 ignored issues
show
Bug introduced by
It seems like $translatorDe defined by $this->objFromFixture(\S...class, 'translator_de') on line 73 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canEdit() does only seem to accept object<SilverStripe\Security\Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
77
        $this->assertTrue($pageEn->canEdit($translatorEn));
0 ignored issues
show
Bug introduced by
It seems like $translatorEn defined by $this->objFromFixture(\S...class, 'translator_en') on line 74 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canEdit() does only seem to accept object<SilverStripe\Security\Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
78
    }
79
}
80