testIfUntranslatableLabelGetsDisabled()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 30
rs 9.7
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Tests\Functional\Controller;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Translation\Bundle\EditInPlace\Activator;
16
use Translation\Bundle\Tests\Functional\BaseTestCase;
17
18
/**
19
 * @author Damien Alexandre <[email protected]>
20
 */
21
class EditInPlaceTest extends BaseTestCase
22
{
23
    public function testActivatedTest(): void
24
    {
25
        $this->bootKernel();
26
        $request = Request::create('/foobar');
27
28
        // Activate the feature
29
        $this->getContainer()->get(Activator::class)->activate();
30
31
        $response = $this->kernel->handle($request);
32
33
        self::assertSame(200, $response->getStatusCode());
34
        self::assertStringContainsString('<!-- TranslationBundle -->', $response->getContent());
35
36
        $dom = new \DOMDocument('1.0', 'utf-8');
37
        @$dom->loadHTML(\mb_convert_encoding($response->getContent(), 'HTML-ENTITIES', 'UTF-8'));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for loadHTML(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

37
        /** @scrutinizer ignore-unhandled */ @$dom->loadHTML(\mb_convert_encoding($response->getContent(), 'HTML-ENTITIES', 'UTF-8'));

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
It seems like mb_convert_encoding($res...TML-ENTITIES', 'UTF-8') can also be of type array; however, parameter $source of DOMDocument::loadHTML() does only seem to accept string, 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

37
        @$dom->loadHTML(/** @scrutinizer ignore-type */ \mb_convert_encoding($response->getContent(), 'HTML-ENTITIES', 'UTF-8'));
Loading history...
38
        $xpath = new \DomXpath($dom);
39
40
        // Check number of x-trans tags
41
        $xtrans = $xpath->query('//x-trans');
42
        self::assertEquals(6, $xtrans->length);
43
44
        // Check attribute with prefix (href="mailto:...")
45
        $emailTag = $dom->getElementById('email');
46
        self::assertEquals('mailto:'.'🚫 Can\'t be translated here. 🚫', $emailTag->getAttribute('href'));
47
        self::assertEquals('localized.email', $emailTag->textContent);
48
49
        // Check attribute
50
        $attributeDiv = $dom->getElementById('attribute-div');
51
        self::assertEquals('🚫 Can\'t be translated here. 🚫', $attributeDiv->getAttribute('data-value'));
52
    }
53
54
    public function testIfUntranslatableLabelGetsDisabled(): void
55
    {
56
        $this->kernel->addConfigFile(__DIR__.'/../app/config/disabled_label.yaml');
57
        $request = Request::create('/foobar');
58
59
        // Activate the feature
60
        $this->bootKernel();
61
        $this->getContainer()->get(Activator::class)->activate();
62
63
        $response = $this->kernel->handle($request);
64
65
        self::assertSame(200, $response->getStatusCode());
66
        self::assertStringContainsString('<!-- TranslationBundle -->', $response->getContent());
67
68
        $dom = new \DOMDocument('1.0', 'utf-8');
69
        @$dom->loadHTML(\mb_convert_encoding($response->getContent(), 'HTML-ENTITIES', 'UTF-8'));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for loadHTML(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

69
        /** @scrutinizer ignore-unhandled */ @$dom->loadHTML(\mb_convert_encoding($response->getContent(), 'HTML-ENTITIES', 'UTF-8'));

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
It seems like mb_convert_encoding($res...TML-ENTITIES', 'UTF-8') can also be of type array; however, parameter $source of DOMDocument::loadHTML() does only seem to accept string, 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

69
        @$dom->loadHTML(/** @scrutinizer ignore-type */ \mb_convert_encoding($response->getContent(), 'HTML-ENTITIES', 'UTF-8'));
Loading history...
70
        $xpath = new \DomXpath($dom);
71
72
        // Check number of x-trans tags
73
        $xtrans = $xpath->query('//x-trans');
74
        self::assertEquals(6, $xtrans->length);
75
76
        // Check attribute with prefix (href="mailto:...")
77
        $emailTag = $dom->getElementById('email');
78
        self::assertEquals('localized.email', $emailTag->getAttribute('href'));
79
        self::assertEquals('localized.email', $emailTag->textContent);
80
81
        // Check attribute
82
        $attributeDiv = $dom->getElementById('attribute-div');
83
        self::assertEquals('translated.attribute', $attributeDiv->getAttribute('data-value'));
84
    }
85
86
    public function testDeactivatedTest(): void
87
    {
88
        $this->bootKernel();
89
        $request = Request::create('/foobar');
90
        $response = $this->kernel->handle($request);
91
92
        self::assertSame(200, $response->getStatusCode());
93
        self::assertStringNotContainsString('x-trans', $response->getContent());
94
    }
95
}
96