Commit d57a0fae authored by Steven Jones's avatar Steven Jones Committed by Steven Jones
Browse files

Issue #2829539 by Steven Jones: Add core unit tests

parent d462a02f
image.effect.image_module_test_ajax:
type: mapping
label: 'Ajax test'
mapping:
test_parameter:
type: integer
label: 'Test Parameter'
image.style.*.third_party.image_module_test:
type: mapping
label: 'Schema for image_module_test module additions to image_style entity'
mapping:
foo:
type: string
label: 'Label for foo'
name: 'Image test'
type: module
description: 'Provides hook implementations for testing Image module functionality.'
package: Testing
# version: VERSION
# core: 8.x
# Information added by Drupal.org packaging script on 2016-09-21
version: '8.2.0-rc2'
core: '8.x'
project: 'drupal'
datestamp: 1474485575
<?php
/**
* @file
* Provides Image module hook implementations for testing purposes.
*/
use Drupal\image\ImageStyleInterface;
function image_module_test_file_download($uri) {
$default_uri = \Drupal::state()->get('image.test_file_download') ?: FALSE;
if ($default_uri == $uri) {
return array('X-Image-Owned-By' => 'image_module_test');
}
}
/**
* Implements hook_image_effect_info_alter().
*
* Used to keep a count of cache misses in \Drupal\image\ImageEffectManager.
*/
function image_module_test_image_effect_info_alter(&$effects) {
$image_effects_definition_called = &drupal_static(__FUNCTION__, 0);
$image_effects_definition_called++;
}
/**
* Implements hook_image_style_presave().
*
* Used to save test third party settings in the image style entity.
*/
function image_module_test_image_style_presave(ImageStyleInterface $style) {
$style->setThirdPartySetting('image_module_test', 'foo', 'bar');
}
<?php
namespace Drupal\image_module_test\Plugin\ImageEffect;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageInterface;
use Drupal\image\ConfigurableImageEffectBase;
/**
* Provides a test effect using Ajax in the configuration form.
*
* @ImageEffect(
* id = "image_module_test_ajax",
* label = @Translation("Ajax test")
* )
*/
class AjaxTestImageEffect extends ConfigurableImageEffectBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'test_parameter' => 0,
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['test_parameter'] = [
'#type' => 'number',
'#title' => t('Test parameter'),
'#default_value' => $this->configuration['test_parameter'],
'#min' => 0,
];
$form['ajax_refresh'] = [
'#type' => 'button',
'#value' => $this->t('Ajax refresh'),
'#ajax' => ['callback' => [$this, 'ajaxCallback']],
];
$form['ajax_value'] = [
'#id' => 'ajax-value',
'#type' => 'item',
'#title' => $this->t('Ajax value'),
'#markup' => 'bar',
];
return $form;
}
/**
* AJAX callback.
*/
public function ajaxCallback($form, FormStateInterface $form_state) {
$item = [
'#type' => 'item',
'#title' => $this->t('Ajax value'),
'#markup' => microtime(),
];
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand('#ajax-value', $item));
return $response;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['test_parameter'] = $form_state->getValue('test_parameter');
}
/**
* {@inheritdoc}
*/
public function applyEffect(ImageInterface $image) {
return TRUE;
}
}
<?php
namespace Drupal\image_module_test\Plugin\ImageEffect;
use Drupal\Core\Image\ImageInterface;
use Drupal\image\ImageEffectBase;
/**
* Performs no operation on an image resource.
*
* @ImageEffect(
* id = "image_module_test_null",
* label = @Translation("Image module test")
* )
*/
class NullTestImageEffect extends ImageEffectBase {
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions, $uri) {
// Unset image dimensions.
$dimensions['width'] = $dimensions['height'] = NULL;
}
/**
* {@inheritdoc}
*/
public function applyEffect(ImageInterface $image) {
return TRUE;
}
}
<?php
namespace Drupal\image_module_test\Plugin\ImageEffect;
use Drupal\Core\Image\ImageInterface;
use Drupal\image\ImageEffectBase;
/**
* Performs an image operation that depends on the URI of the original image.
*
* @ImageEffect(
* id = "image_module_test_uri_dependent",
* label = @Translation("URI dependent test image effect")
* )
*/
class UriDependentTestImageEffect extends ImageEffectBase {
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions, $uri) {
$dimensions = $this->getUriDependentDimensions($uri);
}
/**
* {@inheritdoc}
*/
public function applyEffect(ImageInterface $image) {
$dimensions = $this->getUriDependentDimensions($image->getSource());
return $image->resize($dimensions['width'], $dimensions['height']);
}
/**
* Make the image dimensions dependent on the image file extension.
*
* @param string $uri
* Original image file URI.
*
* @return array
* Associative array.
* - width: Integer with the derivative image width.
* - height: Integer with the derivative image height.
*/
protected function getUriDependentDimensions($uri) {
$dimensions = [];
$extension = pathinfo($uri, PATHINFO_EXTENSION);
switch (strtolower($extension)) {
case 'png':
$dimensions['width'] = $dimensions['height'] = 100;
break;
case 'gif':
$dimensions['width'] = $dimensions['height'] = 50;
break;
default:
$dimensions['width'] = $dimensions['height'] = 20;
break;
}
return $dimensions;
}
}
name: 'Image test views'
type: module
description: 'Provides default views for views image tests.'
package: Testing
# version: VERSION
# core: 8.x
dependencies:
- image
- views
# Information added by Drupal.org packaging script on 2016-09-21
version: '8.2.0-rc2'
core: '8.x'
project: 'drupal'
datestamp: 1474485575
langcode: en
status: true
dependencies:
module:
- file
- user
id: test_image_user_image_data
label: test_image_user_image_data
module: views
description: ''
tag: ''
base_table: users_field_data
base_field: uid
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
access:
type: perm
options:
perm: 'access user profiles'
cache:
type: tag
style:
type: table
options:
grouping: { }
row_class: ''
default_row_class: true
override: true
sticky: false
caption: ''
summary: ''
description: ''
columns:
name: name
fid: fid
info:
name:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
fid:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
default: '-1'
empty_table: false
row:
type: fields
options:
inline: { }
separator: ''
hide_empty: false
default_field_elements: true
relationships:
user_picture_target_id:
id: user_picture_target_id
table: user__user_picture
field: user_picture_target_id
relationship: none
group_type: group
admin_label: 'image from user_picture'
required: true
plugin_id: standard
arguments: { }
display_extenders: { }
<?php
namespace Drupal\Tests\imageapi_optimize\Kernel;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
/**
* Tests the image field rendering using entity fields of the image field type.
*
* @group image
*/
class ImageFormatterTest extends FieldKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('file', 'image', 'imageapi_optimize');
/**
* @var string
*/
protected $entityType;
/**
* @var string
*/
protected $bundle;
/**
* @var string
*/
protected $fieldName;
/**
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
*/
protected $display;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(['field']);
$this->installEntitySchema('entity_test');
$this->installEntitySchema('file');
$this->installSchema('file', array('file_usage'));
$this->entityType = 'entity_test';
$this->bundle = $this->entityType;
$this->fieldName = Unicode::strtolower($this->randomMachineName());
FieldStorageConfig::create(array(
'entity_type' => $this->entityType,
'field_name' => $this->fieldName,
'type' => 'image',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
))->save();
FieldConfig::create([
'entity_type' => $this->entityType,
'field_name' => $this->fieldName,
'bundle' => $this->bundle,
'settings' => [
'file_extensions' => 'jpg',
],
])->save();
$this->display = entity_get_display($this->entityType, $this->bundle, 'default')
->setComponent($this->fieldName, [
'type' => 'image',
'label' => 'hidden',
]);
$this->display->save();
}
/**
* Tests the cache tags from image formatters.
*/
function testImageFormatterCacheTags() {
// Create a test entity with the image field set.
$entity = EntityTest::create([
'name' => $this->randomMachineName(),
]);
$entity->{$this->fieldName}->generateSampleItems(2);
$entity->save();
// Generate the render array to verify if the cache tags are as expected.
$build = $this->display->build($entity);
$this->assertEquals($entity->{$this->fieldName}[0]->entity->getCacheTags(), $build[$this->fieldName][0]['#cache']['tags'], 'First image cache tags is as expected');
$this->assertEquals($entity->{$this->fieldName}[1]->entity->getCacheTags(), $build[$this->fieldName][1]['#cache']['tags'], 'Second image cache tags is as expected');
}
}
<?php
namespace Drupal\Tests\imageapi_optimize\Kernel;
use Drupal\image\Entity\ImageStyle;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests config import for Image styles.
*
* @group image
*/
class ImageImportTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system', 'image', 'image_module_test', 'imageapi_optimize'];
/**
* Tests importing image styles.
*/
public function testImport() {
$style = ImageStyle::create([
'name' => 'test'
]);
$style->addImageEffect(['id' => 'image_module_test_null']);
$style->addImageEffect(['id' => 'image_module_test_null']);
$style->save();
$this->assertEqual(count($style->getEffects()), 2);
$uuid = \Drupal::service('uuid')->generate();
$style->set('effects', [
$uuid => [
'id' => 'image_module_test_null',
],
]);
$style->save();
$style = ImageStyle::load('test');
$this->assertEqual(count($style->getEffects()), 1);
}
}
<?php
namespace Drupal\Tests\imageapi_optimize\Kernel;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
/**
* Tests using entity fields of the image field type.
*
* @group image
*/
class ImageItemTest extends FieldKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('file', 'image', 'imageapi_optimize');
/**
* Created file entity.
*
* @var \Drupal\file\Entity\File
*/
protected $image;
/**
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
protected function setUp() {
parent::setUp();
$this->installEntitySchema('file');
$this->installSchema('file', array('file_usage'));
FieldStorageConfig::create(array(
'entity_type' => 'entity_test',
'field_name' => 'image_test',
'type' => 'image',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
))->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'image_test',
'bundle' => 'entity_test',
'settings' => [
'file_extensions' => 'jpg',
],
])->save();
file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
$this->image = File::create([
'uri' => 'public://example.jpg',
]);
$this->image->save();
$this->imageFactory = $this->container->get('image.factory');
}
/**
* Tests using entity fields of the image field type.
*/
public function testImageItem() {
// Create a test entity with the image field set.
$entity = EntityTest::create();
$entity->image_test->target_id = $this->image->id();
$entity->image_test->alt = $alt = $this->randomMachineName();
$entity->image_test->title = $title = $this->randomMachineName();
$entity->name->value = $this->randomMachineName();
$entity->save();
$entity = EntityTest::load($entity->id());
$this->assertTrue($entity->image_test instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->image_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->image_test->target_id, $this->image->id());
$this->assertEqual($entity->image_test->alt, $alt);
$this->assertEqual($entity->image_test->title, $title);
$image = $this->imageFactory->get('public://example.jpg');
$this->assertEqual($entity->image_test->width, $image->getWidth());
$this->assertEqual($entity->image_test->height, $image->getHeight());
$this->assertEqual($entity->image_test->entity->id(), $this->image->id());
$this->assertEqual($entity->image_test->entity->uuid(), $this->image->uuid());
// Make sure the computed entity reflects updates to the referenced file.
file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example-2.jpg');
$image2 = File::create([
'uri' => 'public://example-2.jpg',
]);
$image2->save();
$entity->image_test->target_id = $image2->id();
$entity->image_test->alt = $new_alt = $this->randomMachineName();
// The width and height is only updated when width is not set.
$entity->image_test->width = NULL;