View source
<?php
namespace Drupal\imagecache_proportions\Tests;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\file\Entity\File;
use Drupal\image\Tests\ImageFieldTestBase;
use Drupal\node\Entity\Node;
class ImagecacheProportionsFormatterTest extends ImageFieldTestBase {
public static $modules = array(
'field_ui',
'imagecache_proportions',
);
public static function getInfo() {
return array(
'name' => 'Imagecache proportion formatter tests',
'description' => 'Test the display of image fields.',
'group' => 'Image',
);
}
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) {
$nid = $this
->uploadNodeImage($image_test['image'], $field_name, 'article');
$node = Node::load($nid, TRUE);
$image_uri = File::load($node->{$field_name}->target_id)
->getFileUri();
$this
->drupalGet(entity_load('image_style', $image_test['style_name'])
->buildUrl($image_uri));
$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.');
$this
->assertEqual(file_get_contents($image_test['image']->uri), $this
->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.');
$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.');
$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.');
}
}
}