You are here

public function TextimageApiTest::testTextimageApi in Textimage 7.3

Test functionality of the API.

File

tests/textimage.test, line 488
Textimage - web test case script.

Class

TextimageApiTest
Functional tests for Textimage API.

Code

public function testTextimageApi() {

  // Add more effects to the style.
  $effect_edits = array();
  $effect_edits[] = array(
    'effect' => 'textimage_text',
    'data' => array(
      'data[settings][font][angle]' => '90',
      'data[settings][font][color][container][hex]' => '#FF0000',
      'data[settings][text_default][text_string]' => 'Eff 1',
    ),
  );
  $effect_edits[] = array(
    'effect' => 'textimage_text',
    'data' => array(
      'data[settings][font][angle]' => '-90',
      'data[settings][font][color][container][hex]' => '#00FF00',
      'data[settings][text_default][text_string]' => 'Eff 2',
    ),
  );
  $effect_edits[] = array(
    'effect' => 'textimage_text',
    'data' => array(
      'data[settings][font][angle]' => '45',
      'data[settings][font][color][container][hex]' => '#0000FF',
      'data[settings][text_default][text_string]' => 'Eff 3',
    ),
  );
  $effect_edits[] = array(
    'effect' => 'image_desaturate',
    'data' => array(),
  );
  $effect_edits[] = array(
    'effect' => 'image_scale_and_crop',
    'data' => array(
      'data[width]' => 120,
      'data[height]' => 121,
    ),
  );
  foreach ($effect_edits as $effect) {
    if (empty($effect['data'])) {
      $this
        ->drupalPost('admin/config/media/image-styles/edit/textimage_test/edit', array(
        'new' => $effect['effect'],
      ), t('Add'));
    }
    else {
      $this
        ->drupalPost('admin/config/media/image-styles/edit/textimage_test/add/' . $effect['effect'], $effect['data'], t('Add effect'));
    }
  }

  // Test Textimage API.
  $textimage = new Textimage();

  // Check API is accepting input, but not providing output, before process.
  $this
    ->assertTextimageException(FALSE, array(
    $textimage,
    'styleByName',
  ), array(
    'textimage_test',
  ));
  $this
    ->assertTextimageException(FALSE, array(
    $textimage,
    'setCaching',
  ), array(
    TRUE,
  ));
  $this
    ->assertNull($textimage
    ->id(), 'ID is not available');
  $this
    ->assertNull($textimage
    ->getUri(), 'URI is not available');
  $this
    ->assertNull($textimage
    ->getUrl(), 'URL is not available');
  $returned_text = $textimage
    ->getText();
  $this
    ->assertTrue(empty($returned_text), 'Processed text is not available');

  // Process Textimage.
  $text_array = array(
    'bingo',
    'bongo',
    'tengo',
    'tango',
  );
  $expected_text_array = 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
    ->assertTrue($textimage
    ->getText() == $expected_text_array, 'Processed text is available');

  // Check API is not allowing changes after processing.
  $this
    ->assertTextimageException(TRUE, array(
    $textimage,
    'styleByName',
  ), array(
    'textimage_test',
  ));
  $this
    ->assertTextimageException(TRUE, array(
    $textimage,
    'effects',
  ), array(
    array(),
  ));
  $this
    ->assertTextimageException(TRUE, array(
    $textimage,
    'extension',
  ), array(
    'png',
  ));
  $this
    ->assertTextimageException(TRUE, array(
    $textimage,
    'setCaching',
  ), array(
    FALSE,
  ));
  $this
    ->assertTextimageException(TRUE, array(
    $textimage,
    'setTargetUri',
  ), array(
    'public://textimage-testing/bingo-bongo.png',
  ));
  $this
    ->assertTextimageException(TRUE, array(
    $textimage,
    'setHashedFilename',
  ), array(
    TRUE,
  ));

  // Check URI.
  $this
    ->assertTrue(strpos($textimage
    ->getUri(), implode('-+-', $expected_text_array)) > 0, 'Human readable filename');

  // Get textimage_store entry.
  $stored_image = db_select('textimage_store', 'ic')
    ->fields('ic')
    ->condition('tiid', $textimage
    ->id(), '=')
    ->execute()
    ->fetchAssoc();
  $image_data = unserialize($stored_image['image_data']);
  $effects_outline = unserialize($stored_image['effects_outline']);

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

  // Check dummy textimage_background effect is not stored in the outline.
  $this
    ->assertTrue(count($effects_outline) == 6, 'Expected number of effects in the outline');
  $is_background = FALSE;
  foreach ($effects_outline as $effect) {
    if ($effect['name'] == 'textimage_background') {
      $is_background = TRUE;
    }
  }
  $this
    ->assertFalse($is_background, 'Dummy textimage_background effect is not stored in the outline');

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

  // Test forced hashed filename.
  $textimage = new Textimage();
  $text_array = array(
    'bingox',
    'bongox',
    'tengox',
    'tangox',
  );
  $expected_text_array = array(
    'bingox',
    'bongox',
    'tengox',
    'tangox',
  );
  $textimage
    ->styleByName('textimage_test')
    ->setHashedFilename(TRUE)
    ->process($text_array);

  // Check URI and Textimage.
  $this
    ->assertTrue(strpos($textimage
    ->getUri(), $textimage
    ->id()) > 0, 'Hashed filename');
  $this
    ->assertTextimage($textimage
    ->getUri(), 120, 121);

  // Test loading the Textimage metadata.
  $id = $textimage
    ->id();
  $uri = $textimage
    ->getUri();
  $textimage = new Textimage();
  $textimage
    ->load($id);

  // Check loaded data.
  $this
    ->assertEqual($textimage
    ->id(), $id, 'Load - ID correct');
  $this
    ->assertEqual($textimage
    ->getUri(), $uri, 'Load - URI correct');
  $this
    ->assertEqual($textimage
    ->getText(), $expected_text_array, 'Load - Text correct');
  $this
    ->assertTextimageException(TRUE, array(
    $textimage,
    'styleByName',
  ), array(
    'textimage_test',
  ));

  // File exists.
  $this
    ->assertTrue(file_exists($uri), 'Load - file exixts');

  // File deletion.
  $this
    ->assertTrue(file_unmanaged_delete($uri), 'Load - file was deleted');

  // Reload and rebuild.
  $textimage = new Textimage();
  $textimage
    ->load($id);
  $this
    ->assertTrue(file_exists($uri), 'Load - file exixts');

  // Test output of theme textimage_formatter.
  $output = theme('textimage_formatter', array(
    'textimage' => $textimage,
    'alt' => 'Alternate text',
    'title' => 'Textimage title',
    'attributes' => array(
      'class' => 'textimage-test',
    ),
    'image_container_attributes' => array(
      'class' => 'textimage-container-test',
    ),
    'href' => $textimage
      ->getUrl(),
  ));
  $this
    ->drupalSetContent($output);
  $elements = $this
    ->xpath('//a[@href = :href]/div[@class = :container_class]/img[@src = :src]', array(
    ':href' => $textimage
      ->getUrl(),
    ':container_class' => 'textimage-container-test',
    ':src' => $textimage
      ->getUrl(),
  ));
  $this
    ->assertTrue(!empty($elements), 'Textimage formatted correctly.');
}