You are here

public function IconTest::testIcon in Embed 8

Tests the icon functionality.

@covers ::convertImageToEncodedData @covers ::convertEncodedDataToImage @covers ::getIconUrl

File

tests/src/Kernel/IconTest.php, line 42

Class

IconTest
Tests embed button icon file handling.

Namespace

Drupal\Tests\embed\Kernel

Code

public function testIcon() {
  $button = EmbedButton::create([
    'id' => 'test',
    'label' => 'Test',
    'type_id' => 'embed_test_default',
  ]);
  $this
    ->assertEmpty($button->icon);
  $this
    ->assertIconUrl('/default.png', $button);
  $uri = 'public://button.png';
  $image_contents = file_get_contents('core/misc/favicon.ico');
  file_put_contents($uri, $image_contents);
  $button
    ->set('icon', EmbedButton::convertImageToEncodedData($uri));
  $this
    ->assertSame([
    'data' => base64_encode($image_contents),
    'uri' => $uri,
  ], $button->icon);
  $this
    ->assertIconUrl($uri, $button);

  // Delete the file and call getIconUrl and test that it recreated the file.
  unlink($uri);
  $this
    ->assertFalse(is_file($uri));
  $this
    ->assertIconUrl($uri, $button);
  $this
    ->assertTrue(is_file($uri));
  $this
    ->assertSame(file_get_contents($uri), file_get_contents('core/misc/favicon.ico'));

  // Test a manual, external URL for the icon image.
  $button
    ->set('icon', [
    'uri' => 'http://www.example.com/button.png',
  ]);
  $this
    ->assertIconUrl('http://www.example.com/button.png', $button);
}