Completed
Pull Request — 2.x (#328)
by
unknown
01:24
created

SeoExtensionTest::testBodyAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\SeoBundle\Tests\Request;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\SeoBundle\Seo\AttributeBag;
18
use Sonata\SeoBundle\Seo\AttributeInterface;
19
use Sonata\SeoBundle\Seo\SeoPageInterface;
20
use Sonata\SeoBundle\Twig\Extension\SeoExtension;
21
22
class SeoExtensionTest extends TestCase
23
{
24
    public function testHtmlAttributes()
25
    {
26
        $page = $this->createMock(SeoPageInterface::class);
27
        $page->expects($this->once())->method('getHtmlAttributes')->will($this->returnValue([
28
            'xmlns' => 'http://www.w3.org/1999/xhtml',
29
            'xmlns:og' => 'http://opengraphprotocol.org/schema/',
30
        ]));
31
32
        $extension = new SeoExtension($page, 'UTF-8');
33
34
        $this->assertSame(
35
            'xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/"',
36
            $extension->getHtmlAttributes()
37
        );
38
    }
39
40
    public function testHeadAttributes()
41
    {
42
        $page = $this->createMock(SeoPageInterface::class);
43
        $page->expects($this->once())->method('getHeadAttributes')->will($this->returnValue([]));
44
45
        $extension = new SeoExtension($page, 'UTF-8');
46
47
        $this->assertSame('', $extension->getHeadAttributes());
48
    }
49
50
    public function testBodyAttributes()
51
    {
52
        $page = $this->createMock([SeoPageInterface::class, AttributeInterface::class]);
53
54
        $page->expects($this->once())->method('bodyAttributes')->willReturn(new AttributeBag());
55
56
        $extension = new SeoExtension($page, 'UTF-8');
57
58
        $this->assertSame('', $extension->getBodyAttributes());
59
    }
60
61
    public function testTitle()
62
    {
63
        $page = $this->createMock(SeoPageInterface::class);
64
        $page->expects($this->once())->method('getTitle')->will($this->returnValue('<b>foo bar</b>'));
65
66
        $extension = new SeoExtension($page, 'UTF-8');
67
68
        $this->assertSame('<title>foo bar</title>', $extension->getTitle());
69
    }
70
71
    public function testEncoding()
72
    {
73
        $page = $this->createMock(SeoPageInterface::class);
74
        $page->expects($this->once())->method('getTitle')->will($this->returnValue('pięć głów zatkniętych na pal'));
75
        $page->expects($this->once())->method('getMetas')->will($this->returnValue([
76
            'http-equiv' => [],
77
            'name' => ['foo' => ['pięć głów zatkniętych na pal', []]],
78
            'schema' => [],
79
            'charset' => [],
80
            'property' => [],
81
        ]));
82
83
        $extension = new SeoExtension($page, 'UTF-8');
84
85
        $this->assertSame('<title>pięć głów zatkniętych na pal</title>', $extension->getTitle());
86
87
        $this->assertSame(
88
            "<meta name=\"foo\" content=\"pięć gł&oacute;w zatkniętych na pal\" />\n",
89
            $extension->getMetadatas()
90
        );
91
    }
92
93
    public function testMetadatas()
94
    {
95
        $page = $this->createMock(SeoPageInterface::class);
96
        $page->expects($this->once())->method('getMetas')->will($this->returnValue([
97
            'http-equiv' => [],
98
            'name' => ['foo' => ['bar "\'"', []]],
99
            'schema' => [],
100
            'charset' => ['UTF-8' => ['', []]],
101
            'property' => [],
102
        ]));
103
104
        $extension = new SeoExtension($page, 'UTF-8');
105
106
        $this->assertSame(
107
            "<meta name=\"foo\" content=\"bar &quot;'&quot;\" />\n<meta charset=\"UTF-8\" />\n",
108
            $extension->getMetadatas()
109
        );
110
    }
111
112
    public function testName()
113
    {
114
        $page = $this->createMock(SeoPageInterface::class);
115
        $extension = new SeoExtension($page, 'UTF-8');
116
117
        $this->assertSame('sonata_seo', $extension->getName());
118
    }
119
120
    public function testLinkCanonical()
121
    {
122
        $page = $this->createMock(SeoPageInterface::class);
123
        $page->expects($this->any())->method('getLinkCanonical')->will($this->returnValue('http://example.com'));
124
125
        $extension = new SeoExtension($page, 'UTF-8');
126
127
        $this->assertSame(
128
            "<link rel=\"canonical\" href=\"http://example.com\"/>\n",
129
            $extension->getLinkCanonical()
130
        );
131
    }
132
133
    public function testLangAlternates()
134
    {
135
        $page = $this->createMock(SeoPageInterface::class);
136
        $page->expects($this->once())->method('getLangAlternates')->will($this->returnValue([
137
                    'http://example.com/' => 'x-default',
138
                ]));
139
140
        $extension = new SeoExtension($page, 'UTF-8');
141
142
        $this->assertSame(
143
            "<link rel=\"alternate\" href=\"http://example.com/\" hreflang=\"x-default\"/>\n",
144
            $extension->getLangAlternates()
145
        );
146
    }
147
148
    public function testOEmbedLinks()
149
    {
150
        $page = $this->createMock(SeoPageInterface::class);
151
        $page->expects($this->once())->method('getOembedLinks')->will($this->returnValue([
152
            'Foo' => 'http://example.com/',
153
        ]));
154
155
        $extension = new SeoExtension($page, 'UTF-8');
156
157
        $this->assertSame(
158
            "<link rel=\"alternate\" type=\"application/json+oembed\" href=\"http://example.com/\" title=\"Foo\" />\n",
159
            $extension->getOembedLinks()
160
        );
161
    }
162
}
163