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