Completed
Pull Request — 2.x (#266)
by Maximilian
01:31
created

SeoExtensionTest::testStructuredData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[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 Sonata\SeoBundle\Tests\Request;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\SeoBundle\Twig\Extension\SeoExtension;
16
17
class SeoExtensionTest extends TestCase
18
{
19
    public function testHtmlAttributes()
20
    {
21
        $page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface');
22
        $page->expects($this->once())->method('getHtmlAttributes')->will(
23
            $this->returnValue(
24
                [
25
                    'xmlns' => 'http://www.w3.org/1999/xhtml',
26
                    'xmlns:og' => 'http://opengraphprotocol.org/schema/',
27
                ]
28
            )
29
        );
30
31
        $extension = new SeoExtension($page, 'UTF-8');
32
33
        $this->assertEquals('xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/"', $extension->getHtmlAttributes());
34
    }
35
36
    public function testHeadAttributes()
37
    {
38
        $page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface');
39
        $page->expects($this->once())->method('getHeadAttributes')->will($this->returnValue([]));
40
41
        $extension = new SeoExtension($page, 'UTF-8');
42
43
        $this->assertEquals('', $extension->getHeadAttributes());
44
    }
45
46
    public function testTitle()
47
    {
48
        $page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface');
49
        $page->expects($this->once())->method('getTitle')->will($this->returnValue('<b>foo bar</b>'));
50
51
        $extension = new SeoExtension($page, 'UTF-8');
52
53
        $this->assertEquals('<title>foo bar</title>', $extension->getTitle());
54
    }
55
56
    public function testEncoding()
57
    {
58
        $page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface');
59
        $page->expects($this->once())->method('getTitle')->will($this->returnValue('pięć głów zatkniętych na pal'));
60
        $page->expects($this->once())->method('getMetas')->will(
61
            $this->returnValue(
62
                [
63
                    'http-equiv' => [],
64
                    'name' => ['foo' => ['pięć głów zatkniętych na pal', []]],
65
                    'schema' => [],
66
                    'charset' => [],
67
                    'property' => [],
68
                ]
69
            )
70
        );
71
72
        $extension = new SeoExtension($page, 'UTF-8');
73
74
        $this->assertEquals('<title>pięć głów zatkniętych na pal</title>', $extension->getTitle());
75
76
        $this->assertEquals("<meta name=\"foo\" content=\"pięć gł&oacute;w zatkniętych na pal\" />\n", $extension->getMetadatas());
77
    }
78
79
    public function testMetadatas()
80
    {
81
        $page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface');
82
        $page->expects($this->once())->method('getMetas')->will(
83
            $this->returnValue(
84
                [
85
                    'http-equiv' => [],
86
                    'name' => ['foo' => ['bar "\'"', []]],
87
                    'schema' => [],
88
                    'charset' => ['UTF-8' => ['', []]],
89
                    'property' => [],
90
                ]
91
            )
92
        );
93
94
        $extension = new SeoExtension($page, 'UTF-8');
95
96
        $this->assertEquals("<meta name=\"foo\" content=\"bar &quot;'&quot;\" />\n<meta charset=\"UTF-8\" />\n", $extension->getMetadatas());
97
    }
98
99
    public function testName()
100
    {
101
        $page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface');
102
        $extension = new SeoExtension($page, 'UTF-8');
103
104
        $this->assertEquals('sonata_seo', $extension->getName());
105
    }
106
107
    public function testLinkCanonical()
108
    {
109
        $page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface');
110
        $page->expects($this->any())->method('getLinkCanonical')->will($this->returnValue('http://example.com'));
111
112
        $extension = new SeoExtension($page, 'UTF-8');
113
114
        $this->assertEquals("<link rel=\"canonical\" href=\"http://example.com\"/>\n", $extension->getLinkCanonical());
115
    }
116
117
    public function testLangAlternates()
118
    {
119
        $page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface');
120
        $page->expects($this->once())->method('getLangAlternates')->will(
121
            $this->returnValue(
122
                [
123
                    'http://example.com/' => 'x-default',
124
                ]
125
            )
126
        );
127
128
        $extension = new SeoExtension($page, 'UTF-8');
129
130
        $this->assertEquals("<link rel=\"alternate\" href=\"http://example.com/\" hreflang=\"x-default\"/>\n", $extension->getLangAlternates());
131
    }
132
133
    public function testOEmbedLinks()
134
    {
135
        $page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface');
136
        $page->expects($this->once())->method('getOembedLinks')->will(
137
            $this->returnValue(
138
                [
139
                    'Foo' => 'http://example.com/',
140
                ]
141
            )
142
        );
143
144
        $extension = new SeoExtension($page, 'UTF-8');
145
146
        $this->assertEquals("<link rel=\"alternate\" type=\"application/json+oembed\" href=\"http://example.com/\" title=\"Foo\" />\n", $extension->getOembedLinks());
147
    }
148
149
    public function testStructuredData()
150
    {
151
        $page = $this->createMock('Sonata\SeoBundle\Seo\SeoPageInterface');
152
        $page->expects($this->any())->method('getStructuredData')->will($this->returnValue(file_get_contents(__DIR__.'/../../Fixtures/structured_data.jsonld')));
153
154
        $extension = new SeoExtension($page, 'UTF-8');
155
156
        $this->assertEquals(
157
'<script type="application/ld+json">{
158
  "@context": "http://schema.org",
159
  "@type": "Organization",
160
  "url": "http://www.example.com",
161
  "name": "Unlimited Ball Bearings Corp.",
162
  "contactPoint": {
163
    "@type": "ContactPoint",
164
    "telephone": "+1-401-555-1212",
165
    "contactType": "Customer service"
166
  }
167
}
168
</script>
169
',
170
            $extension->getStructuredData()
171
        );
172
    }
173
}
174