|
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\SeoPageInterface; |
|
18
|
|
|
use Sonata\SeoBundle\Twig\Extension\SeoExtension; |
|
19
|
|
|
|
|
20
|
|
|
class SeoExtensionTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testHtmlAttributes() |
|
23
|
|
|
{ |
|
24
|
|
|
$page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface'); |
|
25
|
|
|
$page->expects($this->once())->method('getHtmlAttributes')->will( |
|
26
|
|
|
$this->returnValue( |
|
27
|
|
|
[ |
|
28
|
|
|
'xmlns' => 'http://www.w3.org/1999/xhtml', |
|
29
|
|
|
'xmlns:og' => 'http://opengraphprotocol.org/schema/', |
|
30
|
|
|
] |
|
31
|
|
|
) |
|
32
|
|
|
); |
|
33
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
|
34
|
|
|
$page->expects($this->once())->method('getHtmlAttributes')->will($this->returnValue([ |
|
35
|
|
|
'xmlns' => 'http://www.w3.org/1999/xhtml', |
|
36
|
|
|
'xmlns:og' => 'http://opengraphprotocol.org/schema/', |
|
37
|
|
|
])); |
|
38
|
|
|
|
|
39
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertSame( |
|
42
|
|
|
'xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/"', |
|
43
|
|
|
$extension->getHtmlAttributes() |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testHeadAttributes() |
|
48
|
|
|
{ |
|
49
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
|
50
|
|
|
$page->expects($this->once())->method('getHeadAttributes')->will($this->returnValue([])); |
|
51
|
|
|
|
|
52
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame('', $extension->getHeadAttributes()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testTitle() |
|
58
|
|
|
{ |
|
59
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
|
60
|
|
|
$page->expects($this->once())->method('getTitle')->will($this->returnValue('<b>foo bar</b>')); |
|
61
|
|
|
|
|
62
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertSame('<title>foo bar</title>', $extension->getTitle()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testEncoding() |
|
68
|
|
|
{ |
|
69
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
|
70
|
|
|
$page->expects($this->once())->method('getTitle')->will($this->returnValue('pięć głów zatkniętych na pal')); |
|
71
|
|
|
$page->expects($this->once())->method('getMetas')->will( |
|
72
|
|
|
$this->returnValue( |
|
73
|
|
|
[ |
|
74
|
|
|
'http-equiv' => [], |
|
75
|
|
|
'name' => ['foo' => ['pięć głów zatkniętych na pal', []]], |
|
76
|
|
|
'schema' => [], |
|
77
|
|
|
'charset' => [], |
|
78
|
|
|
'property' => [], |
|
79
|
|
|
] |
|
80
|
|
|
) |
|
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łó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 "'"\" />\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('Sonata\SeoBundle\Seo\SeoPageInterface'); |
|
136
|
|
|
$page->expects($this->once())->method('getLangAlternates')->will( |
|
137
|
|
|
$this->returnValue( |
|
138
|
|
|
[ |
|
139
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
|
|
|
|
|
|
140
|
|
|
$page->expects($this->once())->method('getLangAlternates')->will($this->returnValue([ |
|
141
|
|
|
'http://example.com/' => 'x-default', |
|
142
|
|
|
] |
|
143
|
|
|
) |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertSame( |
|
149
|
|
|
"<link rel=\"alternate\" href=\"http://example.com/\" hreflang=\"x-default\"/>\n", |
|
150
|
|
|
$extension->getLangAlternates() |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function testOEmbedLinks() |
|
155
|
|
|
{ |
|
156
|
|
|
$page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface'); |
|
157
|
|
|
$page->expects($this->once())->method('getOembedLinks')->will( |
|
158
|
|
|
$this->returnValue( |
|
159
|
|
|
[ |
|
160
|
|
|
'Foo' => 'http://example.com/', |
|
161
|
|
|
] |
|
162
|
|
|
) |
|
163
|
|
|
); |
|
164
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
|
165
|
|
|
$page->expects($this->once())->method('getOembedLinks')->will($this->returnValue([ |
|
166
|
|
|
'Foo' => 'http://example.com/', |
|
167
|
|
|
])); |
|
168
|
|
|
|
|
169
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertSame( |
|
172
|
|
|
"<link rel=\"alternate\" type=\"application/json+oembed\" href=\"http://example.com/\" title=\"Foo\" />\n", |
|
173
|
|
|
$extension->getOembedLinks() |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|