You are here

function ImagecacheProportionsFormatterTest::testImagecacheProportionsFormatter in Imagecache Proportions 8

Test image formatters on node display.

File

src/Tests/ImagecacheProportionsFormatterTest.php, line 38
Definition of Drupal\imagecache_proportions\Tests\ImagecacheProportionsFormatterTest.

Class

ImagecacheProportionsFormatterTest
Test class to check that formatters and display settings are working.

Namespace

Drupal\imagecache_proportions\Tests

Code

function testImagecacheProportionsFormatter() {
  $scheme = 'public';
  $field_name = strtolower($this
    ->randomName());
  $this
    ->createImageField($field_name, 'article', array(
    'uri_scheme' => $scheme,
  ));
  drupal_get_path('module', 'imagecache_proportions');
  $image_path = drupal_get_path('module', 'imagecache_proportions') . '/files';
  $files = file_scan_directory($image_path, '/^.*\\.(jpg|png)$/');
  foreach ($files as $file) {
    file_unmanaged_copy($file->uri, PublicStream::basePath());
  }
  $image_tests = array(
    'squared' => array(
      'image' => (object) array(
        'uri' => 'public://proportions-squared.jpg',
      ),
      'dimensions' => array(
        'width' => 192,
        'height' => 220,
      ),
      'style_name' => 'medium',
    ),
    'landscape' => array(
      'image' => (object) array(
        'uri' => 'public://proportions-landscape.jpg',
      ),
      'dimensions' => array(
        'width' => 100,
        'height' => 70,
      ),
      'style_name' => 'thumbnail',
    ),
    'portrait' => array(
      'image' => (object) array(
        'uri' => 'public://proportions-portrait.png',
      ),
      'dimensions' => array(
        'width' => 420,
        'height' => 480,
      ),
      'style_name' => 'large',
    ),
  );
  foreach ($image_tests as $image_test) {

    // Create a new node with an image attached.
    $nid = $this
      ->uploadNodeImage($image_test['image'], $field_name, 'article');
    $node = Node::load($nid, TRUE);

    // Test that the default formatter is being used.
    $image_uri = File::load($node->{$field_name}->target_id)
      ->getFileUri();

    // Ensure the derivative image is generated so we do not have to deal with
    // image style callback paths.
    $this
      ->drupalGet(entity_load('image_style', $image_test['style_name'])
      ->buildUrl($image_uri));

    // Test the image linked to file formatter.
    $display_options = array(
      'type' => 'imagecache_proportions',
      'settings' => array(
        'image_link' => 'file',
        'image_style' => 'medium',
        'image_style_portrait' => 'large',
        'image_style_landscape' => 'thumbnail',
        'looseness' => 30,
      ),
    );
    $display = entity_get_display('node', $node
      ->getType(), 'default');
    $display
      ->setComponent($field_name, $display_options)
      ->save();
    $image = array(
      '#theme' => 'image_style',
      '#uri' => $image_uri,
      '#width' => $image_test['dimensions']['width'],
      '#height' => $image_test['dimensions']['height'],
      '#style_name' => $image_test['style_name'],
    );
    $default_output = l($image, file_create_url($image_uri), array(
      'html' => TRUE,
    ));
    $this
      ->drupalGet('node/' . $nid);
    $this
      ->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.');

    // Verify that the image can be downloaded.
    $this
      ->assertEqual(file_get_contents($image_test['image']->uri), $this
      ->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.');

    // Test the image linked to content formatter.
    $display_options['settings']['image_link'] = 'content';
    $display
      ->setComponent($field_name, $display_options)
      ->save();
    $default_output = l($image, 'node/' . $nid, array(
      'html' => TRUE,
    ));
    $this
      ->drupalGet('node/' . $nid);
    $this
      ->assertRaw($default_output, 'Image linked to content formatter displaying correctly on full node view.');

    // Test the image style 'thumbnail' formatter.
    $display_options['settings']['image_link'] = '';
    $display
      ->setComponent($field_name, $display_options)
      ->save();
    $default_output = drupal_render($image);
    $this
      ->drupalGet('node/' . $nid);
    $this
      ->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.');
  }
}