View source
<?php
namespace Drupal\image\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\file\Entity\File;
class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
public static $modules = array(
'field_ui',
);
public function testDefaultImages() {
$node_storage = $this->container
->get('entity.manager')
->getStorage('node');
$files = $this
->drupalGetTestFiles('image');
for ($i = 1; $i <= 10; $i++) {
$filename = $this
->randomMachineName() . "{$i}";
$desired_filepath = 'public://' . $filename;
file_unmanaged_copy($files[0]->uri, $desired_filepath, FILE_EXISTS_ERROR);
$file = entity_create('file', array(
'uri' => $desired_filepath,
'filename' => $filename,
'name' => $filename,
));
$file
->save();
}
$default_images = array();
foreach (array(
'field',
'field',
'field2',
'field_new',
'field_new',
) as $image_target) {
$file = entity_create('file', (array) array_pop($files));
$file
->save();
$default_images[$image_target] = $file;
}
$field_name = strtolower($this
->randomMachineName());
$storage_settings['default_image'] = array(
'uuid' => $default_images['field']
->uuid(),
'alt' => '',
'title' => '',
'width' => 0,
'height' => 0,
);
$field_settings['default_image'] = array(
'uuid' => $default_images['field']
->uuid(),
'alt' => '',
'title' => '',
'width' => 0,
'height' => 0,
);
$widget_settings = array(
'preview_image_style' => 'medium',
);
$field = $this
->createImageField($field_name, 'article', $storage_settings, $field_settings, $widget_settings);
$this
->assertEqual($field
->getSetting('default_image')['uuid'], $default_images['field']
->uuid());
$this
->assertEqual($field
->getSettings()['default_image']['uuid'], $default_images['field']
->uuid());
$field_storage = $field
->getFieldStorageDefinition();
$this
->assertEqual($field_storage
->getSetting('default_image')['uuid'], $default_images['field']
->uuid());
$this
->assertEqual($field_storage
->getSettings()['default_image']['uuid'], $default_images['field']
->uuid());
$field2 = entity_create('field_config', array(
'field_storage' => $field_storage,
'bundle' => 'page',
'label' => $field
->label(),
'required' => $field
->isRequired(),
'settings' => array(
'default_image' => array(
'uuid' => $default_images['field2']
->uuid(),
'alt' => '',
'title' => '',
'width' => 0,
'height' => 0,
),
),
));
$field2
->save();
$widget_settings = entity_get_form_display('node', $field
->getTargetBundle(), 'default')
->getComponent($field_name);
entity_get_form_display('node', 'page', 'default')
->setComponent($field_name, $widget_settings)
->save();
entity_get_display('node', 'page', 'default')
->setComponent($field_name)
->save();
$field_id = $field
->id();
$this
->drupalGet("admin/structure/types/manage/article/fields/{$field_id}/storage");
$this
->assertFieldByXpath('//input[@name="settings[default_image][uuid][fids]"]', $default_images['field']
->id(), format_string('Article image field default equals expected file ID of @fid.', array(
'@fid' => $default_images['field']
->id(),
)));
$this
->drupalGet("admin/structure/types/manage/article/fields/{$field_id}");
$this
->assertFieldByXpath('//input[@name="settings[default_image][uuid][fids]"]', $default_images['field']
->id(), format_string('Article image field field default equals expected file ID of @fid.', array(
'@fid' => $default_images['field']
->id(),
)));
$this
->drupalGet("admin/structure/types/manage/page/fields/{$field_id}/storage");
$this
->assertFieldByXpath('//input[@name="settings[default_image][uuid][fids]"]', $default_images['field']
->id(), format_string('Page image field default equals expected file ID of @fid.', array(
'@fid' => $default_images['field']
->id(),
)));
$field2_id = $field2
->id();
$this
->drupalGet("admin/structure/types/manage/page/fields/{$field2_id}");
$this
->assertFieldByXpath('//input[@name="settings[default_image][uuid][fids]"]', $default_images['field2']
->id(), format_string('Page image field field default equals expected file ID of @fid.', array(
'@fid' => $default_images['field2']
->id(),
)));
$article = $this
->drupalCreateNode(array(
'type' => 'article',
));
$article_built = $this
->drupalBuildEntityView($article);
$this
->assertEqual($article_built[$field_name][0]['#item']->target_id, $default_images['field']
->id(), format_string('A new article node without an image has the expected default image file ID of @fid.', array(
'@fid' => $default_images['field']
->id(),
)));
$page = $this
->drupalCreateNode(array(
'type' => 'page',
));
$page_built = $this
->drupalBuildEntityView($page);
$this
->assertEqual($page_built[$field_name][0]['#item']->target_id, $default_images['field2']
->id(), format_string('A new page node without an image has the expected default image file ID of @fid.', array(
'@fid' => $default_images['field2']
->id(),
)));
$default_image_settings = $field_storage
->getSetting('default_image');
$default_image_settings['uuid'] = $default_images['field_new']
->uuid();
$field_storage
->setSetting('default_image', $default_image_settings);
$field_storage
->save();
$this
->drupalGet("admin/structure/types/manage/article/fields/{$field_id}/storage");
$this
->assertFieldByXpath('//input[@name="settings[default_image][uuid][fids]"]', $default_images['field_new']
->id(), format_string('Updated image field default equals expected file ID of @fid.', array(
'@fid' => $default_images['field_new']
->id(),
)));
$node_storage
->resetCache(array(
$article
->id(),
$page
->id(),
));
$article_built = $this
->drupalBuildEntityView($article = $node_storage
->load($article
->id()));
$page_built = $this
->drupalBuildEntityView($page = $node_storage
->load($page
->id()));
$this
->assertEqual($article_built[$field_name][0]['#item']->target_id, $default_images['field']
->id(), format_string('An existing article node without an image has the expected default image file ID of @fid.', array(
'@fid' => $default_images['field']
->id(),
)));
$this
->assertEqual($page_built[$field_name][0]['#item']->target_id, $default_images['field2']
->id(), format_string('An existing page node without an image has the expected default image file ID of @fid.', array(
'@fid' => $default_images['field2']
->id(),
)));
$default_image_settings = $field
->getSetting('default_image');
$default_image_settings['uuid'] = $default_images['field_new']
->uuid();
$field
->setSetting('default_image', $default_image_settings);
$field
->save();
$this
->drupalGet("admin/structure/types/manage/article/fields/{$field_id}");
$this
->assertFieldByXpath('//input[@name="settings[default_image][uuid][fids]"]', $default_images['field_new']
->id(), format_string('Updated article image field field default equals expected file ID of @fid.', array(
'@fid' => $default_images['field_new']
->id(),
)));
$node_storage
->resetCache(array(
$article
->id(),
$page
->id(),
));
$article_built = $this
->drupalBuildEntityView($article = $node_storage
->load($article
->id()));
$page_built = $this
->drupalBuildEntityView($page = $node_storage
->load($page
->id()));
$this
->assertEqual($article_built[$field_name][0]['#item']->target_id, $default_images['field_new']
->id(), format_string('An existing article node without an image has the expected default image file ID of @fid.', array(
'@fid' => $default_images['field_new']
->id(),
)));
$this
->assertEqual($page_built[$field_name][0]['#item']->target_id, $default_images['field2']
->id(), format_string('An existing page node without an image has the expected default image file ID of @fid.', array(
'@fid' => $default_images['field2']
->id(),
)));
$file = File::load($default_images['field_new']
->id());
$this
->drupalGet('node/add/article');
$this
->assertRaw($file
->getFilename());
$default_image_settings = $field
->getSetting('default_image');
$default_image_settings['uuid'] = 0;
$field
->setSetting('default_image', $default_image_settings);
$field
->save();
$this
->drupalGet("admin/structure/types/manage/article/fields/{$field_id}");
$this
->assertFieldByXpath('//input[@name="settings[default_image][uuid][fids]"]', '', 'Updated article image field field default has been successfully removed.');
$node_storage
->resetCache(array(
$article
->id(),
$page
->id(),
));
$article_built = $this
->drupalBuildEntityView($article = $node_storage
->load($article
->id()));
$page_built = $this
->drupalBuildEntityView($page = $node_storage
->load($page
->id()));
$this
->assertEqual($article_built[$field_name][0]['#item']->target_id, $default_images['field_new']
->id(), format_string('An existing article node without an image has the expected default image file ID of @fid.', array(
'@fid' => $default_images['field_new']
->id(),
)));
$this
->assertEqual($page_built[$field_name][0]['#item']->target_id, $default_images['field2']
->id(), format_string('An existing page node without an image has the expected default image file ID of @fid.', array(
'@fid' => $default_images['field2']
->id(),
)));
$non_image = $this
->drupalGetTestFiles('text');
$this
->drupalPostForm(NULL, array(
'files[settings_default_image_uuid]' => drupal_realpath($non_image[0]->uri),
), t("Upload"));
$this
->assertText('The specified file text-0.txt could not be uploaded.');
$this
->assertText('Only files with the following extensions are allowed: png gif jpg jpeg.');
$file = File::load($default_images['field_new']
->id());
$this
->drupalGet('node/add/article');
$this
->assertRaw($file
->getFilename());
}
public function testInvalidDefaultImage() {
$field_storage = entity_create('field_storage_config', array(
'field_name' => Unicode::strtolower($this
->randomMachineName()),
'entity_type' => 'node',
'type' => 'image',
'settings' => array(
'default_image' => array(
'uuid' => 100000,
),
),
));
$field_storage
->save();
$settings = $field_storage
->getSettings();
$this
->assertNull($settings['default_image']['uuid']);
$field = entity_create('field_config', array(
'field_storage' => $field_storage,
'bundle' => 'page',
'label' => $this
->randomMachineName(),
'settings' => array(
'default_image' => array(
'uuid' => 100000,
),
),
));
$field
->save();
$settings = $field
->getSettings();
$this
->assertNull($settings['default_image']['uuid']);
}
}