1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MOrtola\BehatSEOContexts\Context; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\Assert; |
6
|
|
|
|
7
|
|
|
class SocialContext extends BaseContext |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @Then /^the (Twitter|Facebook) Open Graph data should not satisfy (minimum|full) requirements$/ |
11
|
|
|
*/ |
12
|
|
|
public function theOpenGraphDataShouldNotSatisfyRequirements(string $socialNetworkName, string $requirementsType) |
13
|
|
|
{ |
14
|
|
|
$this->assertInverse( |
15
|
|
|
function () use ($socialNetworkName, $requirementsType) { |
16
|
|
|
$this->theOpenGraphDataShouldSatisfyRequirements($socialNetworkName, $requirementsType); |
17
|
|
|
}, |
18
|
|
|
sprintf('The %s OG Data satisfies %s requirements.', $socialNetworkName, $requirementsType) |
19
|
|
|
); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @throws \Exception |
24
|
|
|
* |
25
|
|
|
* @Then /^the (Twitter|Facebook) Open Graph data should satisfy (minimum|full) requirements$/ |
26
|
|
|
*/ |
27
|
|
|
public function theOpenGraphDataShouldSatisfyRequirements(string $socialNetworkName, string $requirementsType) |
28
|
|
|
{ |
29
|
|
|
switch ($socialNetworkName) { |
30
|
|
|
case 'Twitter': |
31
|
|
|
$this->validateTwitterOpenGraphData('full' === $requirementsType); |
32
|
|
|
|
33
|
|
|
break; |
34
|
|
|
case 'Facebook': |
35
|
|
|
$this->validateFacebookOpenGraphData('full' === $requirementsType); |
36
|
|
|
|
37
|
|
|
break; |
38
|
|
|
default: |
39
|
|
|
throw new \Exception( |
40
|
|
|
sprintf('%s open graph validation is not allowed.', $socialNetworkName) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
|
|
private function validateTwitterOpenGraphData(bool $fullRequirements = false) |
49
|
|
|
{ |
50
|
|
|
Assert::assertContains( |
51
|
|
|
$this->getOGMetaContent('twitter:card'), |
52
|
|
|
['summary', 'summary_large_image', 'app', 'player'], |
53
|
|
|
'OG meta twitter:card contains invalid content' |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->getOGMetaContent('twitter:title'); |
57
|
|
|
|
58
|
|
|
if ($fullRequirements) { |
59
|
|
|
Assert::assertNotFalse( |
60
|
|
|
filter_var($this->getOGMetaContent('twitter:image'), FILTER_VALIDATE_URL) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
Assert::assertContains( |
64
|
|
|
pathinfo($this->getOGMetaContent('twitter:image'))['extension'], |
65
|
|
|
['jpg', 'jpeg', 'webp', 'png', 'gif'], |
66
|
|
|
'OG meta twitter:image has valid extension. Allowed are: jpg/jpeg, png, webp, gif' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$this->getOGMetaContent('twitter:description'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @throws \Exception |
75
|
|
|
*/ |
76
|
|
|
private function getOGMetaContent(string $property): string |
77
|
|
|
{ |
78
|
|
|
$ogMeta = $this->getSession()->getPage()->find( |
79
|
|
|
'xpath', |
80
|
|
|
sprintf('//head/meta[@property="%1$s" or @name="%1$s"]', $property) |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
Assert::assertNotNull( |
84
|
|
|
$ogMeta, |
85
|
|
|
sprintf('Open Graph meta %s does not exist', $property) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
Assert::assertNotEmpty( |
89
|
|
|
$ogMeta->getAttribute('content'), |
90
|
|
|
sprintf('Open Graph meta %s should not be empty', $property) |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return $ogMeta->getAttribute('content'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
private function validateFacebookOpenGraphData(bool $fullRequirements = false) |
100
|
|
|
{ |
101
|
|
|
Assert::assertNotFalse( |
102
|
|
|
filter_var($this->getOGMetaContent('og:url'), FILTER_VALIDATE_URL) |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
Assert::assertEquals( |
106
|
|
|
$this->getOGMetaContent('og:url'), |
107
|
|
|
$this->getCurrentUrl(), |
108
|
|
|
'OG meta og:url does not match expected url' |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->getOGMetaContent('og:title'); |
112
|
|
|
$this->getOGMetaContent('og:description'); |
113
|
|
|
|
114
|
|
|
Assert::assertNotFalse( |
115
|
|
|
filter_var($this->getOGMetaContent('og:image'), FILTER_VALIDATE_URL) |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
Assert::assertContains( |
119
|
|
|
pathinfo($this->getOGMetaContent('og:image'))['extension'], |
120
|
|
|
['jpg', 'jpeg', 'png', 'gif'], |
121
|
|
|
'OG meta og:image has valid extension. Allowed are: jpg/jpeg, png, gif' |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
if ($fullRequirements) { |
125
|
|
|
Assert::assertContains( |
126
|
|
|
$this->getOGMetaContent('og:type'), |
127
|
|
|
[ |
128
|
|
|
'article', |
129
|
|
|
'book', |
130
|
|
|
'books.author', |
131
|
|
|
'books.book', |
132
|
|
|
'books.genre', |
133
|
|
|
'business.business', |
134
|
|
|
'fitness.course', |
135
|
|
|
'game.achievement', |
136
|
|
|
'music.album', |
137
|
|
|
'music.playlist', |
138
|
|
|
'music.radio_station', |
139
|
|
|
'music.song', |
140
|
|
|
'place', |
141
|
|
|
'product', |
142
|
|
|
'product.group', |
143
|
|
|
'product.item', |
144
|
|
|
'profile', |
145
|
|
|
'restaurant.menu', |
146
|
|
|
'restaurant.menu_item', |
147
|
|
|
'restaurant.menu_section', |
148
|
|
|
'restaurant.restaurant', |
149
|
|
|
'video.episode', |
150
|
|
|
'video.movie', |
151
|
|
|
'video.other', |
152
|
|
|
'video.tv_show', |
153
|
|
|
], |
154
|
|
|
'OG meta og:type contains invalid content.' |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
Assert::assertRegExp( |
158
|
|
|
'/^[a-z]{2}_[A-Z]{2}$/', |
159
|
|
|
$this->getOGMetaContent('og:locale'), |
160
|
|
|
'OG meta og:locale does not follow the right format az_AZ.' |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|