ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
OpenGraphMetadataTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
28
32class OpenGraphMetadataTest extends TestCase
33{
34 protected Factory $factory;
35
36 protected function setUp(): void
37 {
38 $this->factory = new Factory();
39 }
40
41 public function testTextTag(): void
42 {
43 $property = 'test_property';
44 $value = 'test_value';
45
46 $link_tag = new Text($property, $value);
47
48 $this->assertEquals(
49 "<meta property=\"$property\" content=\"$value\" />",
50 $link_tag->toHtml()
51 );
52 }
53
54 public function testLinkTag(): void
55 {
56 $property = 'test_property';
57 $value = 'test_value';
58
59 $link_tag = new Link($property, $this->getMockedUrl($value));
60
61 $this->assertEquals(
62 "<meta property=\"$property\" content=\"$value\" />",
63 $link_tag->toHtml()
64 );
65 }
66
67 public function testAudioTag(): void
68 {
69 $expected_url = 'test_url';
70 $expected_mime = 'test_mime_type';
71 $expected_html =
72 "<meta property=\"og:audio\" content=\"$expected_url\" />" . PHP_EOL .
73 "<meta property=\"og:audio:type\" content=\"$expected_mime\" />" . PHP_EOL;
74
75 $audio_tag = $this->factory->audio($this->getMockedUrl($expected_url), $expected_mime);
76
77 $this->assertEquals($expected_html, $audio_tag->toHtml());
78 }
79
80 public function testImageTag(): void
81 {
82 $expected_url = 'test_url';
83 $expected_mime = 'test_mime_type';
84 $expected_alt = 'test_aria_label';
85 $expected_width = 200;
86 $expected_height = 100;
87 $expected_html =
88 "<meta property=\"og:image\" content=\"$expected_url\" />" . PHP_EOL .
89 "<meta property=\"og:image:type\" content=\"$expected_mime\" />" . PHP_EOL .
90 "<meta property=\"og:image:alt\" content=\"$expected_alt\" />" . PHP_EOL .
91 "<meta property=\"og:image:width\" content=\"$expected_width\" />" . PHP_EOL .
92 "<meta property=\"og:image:height\" content=\"$expected_height\" />" . PHP_EOL;
93
94 $image_tag = $this->factory->image(
95 $this->getMockedUrl($expected_url),
96 $expected_mime,
97 $expected_alt,
98 $expected_width,
99 $expected_height
100 );
101
102 $this->assertEquals($expected_html, $image_tag->toHtml());
103 }
104
105 public function testVideoTag(): void
106 {
107 $expected_url = 'test_url';
108 $expected_mime = 'test_mime_type';
109 $expected_width = 200;
110 $expected_height = 100;
111 $expected_html =
112 "<meta property=\"og:video\" content=\"$expected_url\" />" . PHP_EOL .
113 "<meta property=\"og:video:type\" content=\"$expected_mime\" />" . PHP_EOL .
114 "<meta property=\"og:video:width\" content=\"$expected_width\" />" . PHP_EOL .
115 "<meta property=\"og:video:height\" content=\"$expected_height\" />" . PHP_EOL;
116
117 $video_tag = $this->factory->video(
118 $this->getMockedUrl($expected_url),
119 $expected_mime,
120 $expected_width,
121 $expected_height
122 );
123
124 $this->assertEquals($expected_html, $video_tag->toHtml());
125 }
126
127 public function testWebsiteTag(): void
128 {
129 $expected_canonical_url = 'test_canonical_url';
130 $expected_image_html = 'test_image_html';
131 $expected_object_title = 'test_object_title';
132 $expected_website_name = 'test_website_name';
133 $expected_description = 'test_description';
134 $expected_locale = 'test_locale';
135 $expected_locale_alt_1 = 'test_locale_alt_1';
136 $expected_locale_alt_2 = 'test_locale_alt_2';
137 $expected_additional_resource_html_1 = 'test_additional_resource_html_1';
138 $expected_additional_resource_html_2 = 'test_additional_resource_html_2';
139
140 $expected_html =
141 "<meta property=\"og:type\" content=\"website\" />" . PHP_EOL .
142 "<meta property=\"og:title\" content=\"test_object_title\" />" . PHP_EOL .
143 "<meta property=\"og:url\" content=\"test_canonical_url\" />" . PHP_EOL .
144 $expected_image_html . PHP_EOL .
145 "<meta property=\"og:site_title\" content=\"test_website_name\" />" . PHP_EOL .
146 "<meta property=\"og:description\" content=\"test_description\" />" . PHP_EOL .
147 "<meta property=\"og:locale\" content=\"test_locale\" />" . PHP_EOL .
148 "<meta property=\"og:locale:alternative\" content=\"test_locale_alt_1\" />" . PHP_EOL .
149 "<meta property=\"og:locale:alternative\" content=\"test_locale_alt_2\" />" . PHP_EOL .
150 $expected_additional_resource_html_1 . PHP_EOL .
151 $expected_additional_resource_html_2 . PHP_EOL;
152
153 $website_tag = $this->factory->website(
154 $this->getMockedUrl($expected_canonical_url),
155 $this->getMockedImage($expected_image_html),
156 $expected_object_title,
157 $expected_website_name,
158 $expected_description,
159 $expected_locale,
160 [
161 $expected_locale_alt_1,
162 $expected_locale_alt_2,
163 ],
164 [
165 $this->getMockedResource($expected_additional_resource_html_1),
166 $this->getMockedResource($expected_additional_resource_html_2),
167 ]
168 );
169
170 $this->assertEquals($expected_html, $website_tag->toHtml());
171 }
172
173 protected function getMockedResource(string $html): Resource
174 {
175 $mock_resource = $this->createMock(Resource::class);
176 $mock_resource->method('toHtml')->willReturn($html);
177 $mock_resource->method('getTags')->willReturnCallback(
178 static function () use ($mock_resource): Generator {
179 yield $mock_resource;
180 }
181 );
182
183 return $mock_resource;
184 }
185
186 protected function getMockedImage(string $html): Image
187 {
188 $mock_image = $this->createMock(Image::class);
189 $mock_image->method('toHtml')->willReturn($html);
190 $mock_image->method('getTags')->willReturnCallback(
191 static function () use ($mock_image): Generator {
192 yield $mock_image;
193 }
194 );
195
196 return $mock_image;
197 }
198
199 protected function getMockedUrl(string $url): URI
200 {
201 $mock_url = $this->createMock(URI::class);
202 $mock_url->method('__toString')->willReturn($url);
203
204 return $mock_url;
205 }
206}
factory()
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
$url
Definition: shib_logout.php:68