You are here

public function TextimageApiTest::testTextimageApi in Textimage 8.4

Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/TextimageApiTest.php \Drupal\Tests\textimage\Kernel\TextimageApiTest::testTextimageApi()

Test basic functionality of the API.

File

tests/src/Kernel/TextimageApiTest.php, line 71

Class

TextimageApiTest
Kernel tests for Textimage API.

Namespace

Drupal\Tests\textimage\Kernel

Code

public function testTextimageApi() {

  // Add more effects to the test style.
  $style = ImageStyle::load('textimage_test');
  $style
    ->addImageEffect([
    'id' => 'image_effects_text_overlay',
    'data' => [
      'font' => [
        'name' => 'Linux Libertine',
        'uri' => drupal_get_path('module', 'image_effects') . '/tests/fonts/LinLibertineTTF_5.3.0_2012_07_02/LinLibertine_Rah.ttf',
        'size' => 16,
        'angle' => '90',
        'color' => '#FF0000',
      ],
      'text_string' => 'Eff 1',
    ],
  ]);
  $style
    ->addImageEffect([
    'id' => 'image_effects_text_overlay',
    'data' => [
      'font' => [
        'name' => 'Linux Libertine',
        'uri' => drupal_get_path('module', 'image_effects') . '/tests/fonts/LinLibertineTTF_5.3.0_2012_07_02/LinLibertine_Rah.ttf',
        'size' => 16,
        'angle' => '-90',
        'color' => '#00FF00',
      ],
      'text_string' => 'Eff 2',
    ],
  ]);
  $style
    ->addImageEffect([
    'id' => 'image_effects_text_overlay',
    'data' => [
      'font' => [
        'name' => 'Linux Libertine',
        'uri' => drupal_get_path('module', 'image_effects') . '/tests/fonts/LinLibertineTTF_5.3.0_2012_07_02/LinLibertine_Rah.ttf',
        'size' => 16,
        'angle' => '45',
        'color' => '#0000FF',
      ],
      'text_string' => 'Eff 3',
    ],
  ]);
  $style
    ->addImageEffect([
    'id' => 'image_desaturate',
    'data' => [],
  ]);
  $style
    ->addImageEffect([
    'id' => 'image_scale_and_crop',
    'data' => [
      'width' => 120,
      'height' => 121,
    ],
  ]);
  $style
    ->save();

  // Test Textimage API.
  $textimage = $this->textimageFactory
    ->get();

  // Check API is accepting input, but not providing output, before process.
  $textimage
    ->setStyle($style);
  $textimage
    ->setTemporary(FALSE);
  $textimage
    ->setTokenData([
    'user' => $this->testUser,
  ]);
  $this
    ->assertNull($textimage
    ->id(), 'ID is not available');
  $this
    ->assertNull($textimage
    ->getUri(), 'URI is not available');
  $this
    ->assertNull($textimage
    ->getUrl(), 'URL is not available');
  $this
    ->assertNull($textimage
    ->getBubbleableMetadata(), 'Bubbleable metadata is not available');
  $this
    ->assertEmpty($textimage
    ->getText(), 'Processed text is not available');
  try {
    $textimage
      ->buildImage();
    $this
      ->fail('buildImage() should have failed.');
  } catch (TextimageException $e) {

    // Countinue.
  }

  // Process Textimage.
  $text_array = [
    'bingo',
    'bongo',
    'tengo',
    'tango',
  ];
  $expected_text_array = [
    'bingo',
    'bongo',
    'tengo',
    'tango',
  ];
  $textimage
    ->process($text_array);

  // Check API is providing output after processing.
  $this
    ->assertNotNull($textimage
    ->id(), 'ID is available');
  $this
    ->assertNotNull($textimage
    ->getUri(), 'URI is available');
  $this
    ->assertNotNull($textimage
    ->getUrl(), 'URL is available');
  $this
    ->assertNotNull($textimage
    ->getBubbleableMetadata(), 'Bubbleable metadata is available');
  $this
    ->assertSame($expected_text_array, $textimage
    ->getText(), 'Processed text is available');

  // Build Textimage.
  $textimage
    ->buildImage();

  // Check API is not allowing changes after processing.
  try {
    $textimage
      ->setStyle($style);
    $this
      ->fail('setStyle should have failed.');
  } catch (TextimageException $e) {

    // Countinue.
  }
  try {
    $textimage
      ->setEffects([]);
    $this
      ->fail('setEffects should have failed.');
  } catch (TextimageException $e) {

    // Countinue.
  }
  try {
    $textimage
      ->setTargetExtension('png');
    $this
      ->fail('setTargetExtension should have failed.');
  } catch (TextimageException $e) {

    // Countinue.
  }
  try {
    $textimage
      ->setTemporary(TRUE);
    $this
      ->fail('setTemporary should have failed.');
  } catch (TextimageException $e) {

    // Countinue.
  }
  try {
    $textimage
      ->setTokenData([
      'user' => $this->testUser,
    ]);
    $this
      ->fail('setTokenData should have failed.');
  } catch (TextimageException $e) {

    // Countinue.
  }
  try {
    $textimage
      ->setTargetUri('public://textimage-testing/bingo-bongo.png');
    $this
      ->fail('setTargetUri should have failed.');
  } catch (TextimageException $e) {

    // Countinue.
  }
  try {
    $textimage
      ->process($text_array);
    $this
      ->fail('Re-processed an already processed Textimage');
  } catch (TextimageException $e) {

    // Countinue.
  }

  // Get textimage cache entry.
  $stored_image = \Drupal::cache('textimage')
    ->get('tiid:' . $textimage
    ->id());
  $image_data = $stored_image->data['imageData'];
  $effects_outline = $stored_image->data['effects'];

  // Check processed text is stored in image data.
  $this
    ->assertSame($expected_text_array, array_values($image_data['text']), 'Processed text stored in image data');

  // Check count of effects is as expected.
  $this
    ->assertCount(6, $effects_outline, 'Expected number of effects in the outline');

  // Check processed text is not stored in the effects outline.
  foreach ($effects_outline as $effect) {
    if ($effect['id'] == 'image_effects_text_overlay') {
      $this
        ->assertTrue(!isset($effect['data']['text_string']), 'Processed text not stored in the effects outline');
    }
  }
}