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\Seo; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\SeoBundle\Seo\SeoPage; |
18
|
|
|
|
19
|
|
|
final class SeoPageTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testAddMeta(): void |
22
|
|
|
{ |
23
|
|
|
$page = new SeoPage(); |
24
|
|
|
$page->addMeta('property', 'foo', 'bar'); |
25
|
|
|
|
26
|
|
|
$expected = [ |
27
|
|
|
'http-equiv' => [], |
28
|
|
|
'name' => [], |
29
|
|
|
'schema' => [], |
30
|
|
|
'charset' => [], |
31
|
|
|
'property' => ['foo' => ['bar', []]], |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
$this->assertSame($expected, $page->getMetas()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testOverrideMetas(): void |
38
|
|
|
{ |
39
|
|
|
$page = new SeoPage(); |
40
|
|
|
$page->setMetas(['property' => ['foo' => 'bar', 'foo2' => ['bar2', []]]]); |
41
|
|
|
|
42
|
|
|
$expected = [ |
43
|
|
|
'property' => ['foo' => ['bar', []], 'foo2' => ['bar2', []]], |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
$this->assertSame($expected, $page->getMetas()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testRemoveMeta(): void |
50
|
|
|
{ |
51
|
|
|
$page = new SeoPage(); |
52
|
|
|
$page->setMetas(['property' => ['foo' => 'bar', 'foo2' => ['bar2', []]]]); |
53
|
|
|
$page->removeMeta('property', 'foo'); |
54
|
|
|
|
55
|
|
|
$this->assertFalse($page->hasMeta('property', 'foo')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testInvalidMetas(): void |
59
|
|
|
{ |
60
|
|
|
$this->expectException(\RuntimeException::class); |
61
|
|
|
|
62
|
|
|
$page = new SeoPage(); |
63
|
|
|
$page->setMetas([ |
64
|
|
|
'type' => 'not an array', |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testHtmlAttributes(): void |
69
|
|
|
{ |
70
|
|
|
$page = new SeoPage(); |
71
|
|
|
$page->setHtmlAttributes(['key1' => 'value1']); |
72
|
|
|
$page->addHtmlAttributes('key2', 'value2'); |
73
|
|
|
|
74
|
|
|
$expected = [ |
75
|
|
|
'key1' => 'value1', |
76
|
|
|
'key2' => 'value2', |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
$this->assertSame($expected, $page->getHtmlAttributes()); |
80
|
|
|
|
81
|
|
|
$this->assertTrue($page->hasHtmlAttribute('key2')); |
82
|
|
|
$page->removeHtmlAttributes('key2'); |
83
|
|
|
$this->assertFalse($page->hasHtmlAttribute('key2')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testHeadAttributes(): void |
87
|
|
|
{ |
88
|
|
|
$page = new SeoPage(); |
89
|
|
|
$page->setHeadAttributes(['head1' => 'value1']); |
90
|
|
|
$page->addHeadAttribute('head2', 'value2'); |
91
|
|
|
|
92
|
|
|
$expected = [ |
93
|
|
|
'head1' => 'value1', |
94
|
|
|
'head2' => 'value2', |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
$this->assertSame($expected, $page->getHeadAttributes()); |
98
|
|
|
|
99
|
|
|
$this->assertTrue($page->hasHeadAttribute('head1')); |
100
|
|
|
$page->removeHeadAttribute('head1'); |
101
|
|
|
$this->assertFalse($page->hasHeadAttribute('head1')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testSetTitle(): void |
105
|
|
|
{ |
106
|
|
|
$page = new SeoPage(); |
107
|
|
|
$page->setTitle('My title'); |
108
|
|
|
|
109
|
|
|
$this->assertSame('My title', $page->getTitle()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testAddTitle(): void |
113
|
|
|
{ |
114
|
|
|
$page = new SeoPage(); |
115
|
|
|
$page->setTitle('My title'); |
116
|
|
|
$page->setSeparator(' - '); |
117
|
|
|
$page->addTitle('Additional title'); |
118
|
|
|
|
119
|
|
|
$this->assertSame('Additional title - My title', $page->getTitle()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testLinkCanonical(): void |
123
|
|
|
{ |
124
|
|
|
$page = new SeoPage(); |
125
|
|
|
$page->setLinkCanonical('http://example.com'); |
126
|
|
|
|
127
|
|
|
$this->assertSame('http://example.com', $page->getLinkCanonical()); |
128
|
|
|
|
129
|
|
|
$page->removeLinkCanonical(); |
130
|
|
|
$this->assertSame('', $page->getLinkCanonical()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testLangAlternates(): void |
134
|
|
|
{ |
135
|
|
|
$page = new SeoPage(); |
136
|
|
|
$page->setLangAlternates(['http://example.com/' => 'x-default']); |
137
|
|
|
$page->addLangAlternate('http://example.com/en-us', 'en-us'); |
138
|
|
|
|
139
|
|
|
$expected = [ |
140
|
|
|
'http://example.com/' => 'x-default', |
141
|
|
|
'http://example.com/en-us' => 'en-us', |
142
|
|
|
]; |
143
|
|
|
|
144
|
|
|
$this->assertSame($expected, $page->getLangAlternates()); |
145
|
|
|
|
146
|
|
|
$this->assertTrue($page->hasLangAlternate('http://example.com/')); |
147
|
|
|
$page->removeLangAlternate('http://example.com/'); |
148
|
|
|
$this->assertFalse($page->hasLangAlternate('http://example.com/')); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* The hasMeta() should return true for a defined meta, false otherwise. |
153
|
|
|
*/ |
154
|
|
|
public function testHasMeta(): void |
155
|
|
|
{ |
156
|
|
|
$page = new SeoPage(); |
157
|
|
|
$page->addMeta('property', 'test', ''); |
158
|
|
|
|
159
|
|
|
$this->assertTrue($page->hasMeta('property', 'test')); |
160
|
|
|
$this->assertFalse($page->hasMeta('property', 'fake')); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testSetSeparator(): void |
164
|
|
|
{ |
165
|
|
|
$page = new SeoPage(); |
166
|
|
|
|
167
|
|
|
$this->assertInstanceOf(SeoPage::class, $page->setSeparator('-')); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|