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(): void |
23
|
|
|
{ |
24
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
25
|
|
|
$page->expects($this->once())->method('getHtmlAttributes')->will($this->returnValue([ |
26
|
|
|
'xmlns' => 'http://www.w3.org/1999/xhtml', |
27
|
|
|
'xmlns:og' => 'http://opengraphprotocol.org/schema/', |
28
|
|
|
])); |
29
|
|
|
|
30
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
31
|
|
|
|
32
|
|
|
$this->assertSame( |
33
|
|
|
'xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/"', |
34
|
|
|
$extension->getHtmlAttributes() |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testHeadAttributes(): void |
39
|
|
|
{ |
40
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
41
|
|
|
$page->expects($this->once())->method('getHeadAttributes')->will($this->returnValue([])); |
42
|
|
|
|
43
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
44
|
|
|
|
45
|
|
|
$this->assertSame('', $extension->getHeadAttributes()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testTitle(): void |
49
|
|
|
{ |
50
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
51
|
|
|
$page->expects($this->once())->method('getTitle')->will($this->returnValue('<b>foo bar</b>')); |
52
|
|
|
|
53
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
54
|
|
|
|
55
|
|
|
$this->assertSame('<title>foo bar</title>', $extension->getTitle()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testEncoding(): void |
59
|
|
|
{ |
60
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
61
|
|
|
$page->expects($this->once())->method('getTitle')->will($this->returnValue('pięć głów zatkniętych na pal')); |
62
|
|
|
$page->expects($this->once())->method('getMetas')->will($this->returnValue([ |
63
|
|
|
'http-equiv' => [], |
64
|
|
|
'name' => ['foo' => ['pięć głów zatkniętych na pal', []]], |
65
|
|
|
'schema' => [], |
66
|
|
|
'charset' => [], |
67
|
|
|
'property' => [], |
68
|
|
|
])); |
69
|
|
|
|
70
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
71
|
|
|
|
72
|
|
|
$this->assertSame('<title>pięć głów zatkniętych na pal</title>', $extension->getTitle()); |
73
|
|
|
|
74
|
|
|
$this->assertSame( |
75
|
|
|
"<meta name=\"foo\" content=\"pięć głów zatkniętych na pal\" />\n", |
76
|
|
|
$extension->getMetadatas() |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testMetadatas(): void |
81
|
|
|
{ |
82
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
83
|
|
|
$page->expects($this->once())->method('getMetas')->will($this->returnValue([ |
84
|
|
|
'http-equiv' => [], |
85
|
|
|
'name' => ['foo' => ['bar "\'"', []]], |
86
|
|
|
'schema' => [], |
87
|
|
|
'charset' => ['UTF-8' => ['', []]], |
88
|
|
|
'property' => [ |
89
|
|
|
'og:image:width' => [848, []], |
90
|
|
|
'og:type' => [new MetaTest(), []], |
91
|
|
|
], |
92
|
|
|
])); |
93
|
|
|
|
94
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
95
|
|
|
|
96
|
|
|
$this->assertSame( |
97
|
|
|
"<meta name=\"foo\" content=\"bar "'"\" />\n<meta charset=\"UTF-8\" />\n<meta property=\"og:image:width\" content=\"848\" />\n<meta property=\"og:type\" content=\"article\" />\n", |
98
|
|
|
$extension->getMetadatas() |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testName(): void |
103
|
|
|
{ |
104
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
105
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
106
|
|
|
|
107
|
|
|
$this->assertSame('sonata_seo', $extension->getName()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testLinkCanonical(): void |
111
|
|
|
{ |
112
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
113
|
|
|
$page->expects($this->any())->method('getLinkCanonical')->will($this->returnValue('http://example.com')); |
114
|
|
|
|
115
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
116
|
|
|
|
117
|
|
|
$this->assertSame( |
118
|
|
|
"<link rel=\"canonical\" href=\"http://example.com\"/>\n", |
119
|
|
|
$extension->getLinkCanonical() |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testLangAlternates(): void |
124
|
|
|
{ |
125
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
126
|
|
|
$page->expects($this->once())->method('getLangAlternates')->will($this->returnValue([ |
127
|
|
|
'http://example.com/' => 'x-default', |
128
|
|
|
])); |
129
|
|
|
|
130
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
131
|
|
|
|
132
|
|
|
$this->assertSame( |
133
|
|
|
"<link rel=\"alternate\" href=\"http://example.com/\" hreflang=\"x-default\"/>\n", |
134
|
|
|
$extension->getLangAlternates() |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testOEmbedLinks(): void |
139
|
|
|
{ |
140
|
|
|
$page = $this->createMock(SeoPageInterface::class); |
141
|
|
|
$page->expects($this->once())->method('getOembedLinks')->will($this->returnValue([ |
142
|
|
|
'Foo' => 'http://example.com/', |
143
|
|
|
])); |
144
|
|
|
|
145
|
|
|
$extension = new SeoExtension($page, 'UTF-8'); |
146
|
|
|
|
147
|
|
|
$this->assertSame( |
148
|
|
|
"<link rel=\"alternate\" type=\"application/json+oembed\" href=\"http://example.com/\" title=\"Foo\" />\n", |
149
|
|
|
$extension->getOembedLinks() |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
class MetaTest |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
public function __toString() |
157
|
|
|
{ |
158
|
|
|
return 'article'; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.