1 image.test | public ImageFieldDefaultImagesTestCase::testDefaultImages() |
Tests CRUD for fields and fields instances with default images.
File
- core/
modules/ image/ tests/ image.test, line 1626 - Tests for image.module.
Class
- ImageFieldDefaultImagesTestCase
- Tests default image settings.
Code
public function testDefaultImages() {
// Create files to use as the default images.
$files = $this->backdropGetTestFiles('image');
$default_images = array();
foreach (array('field', 'instance', 'instance2', 'field_new', 'instance_new') as $image_target) {
$file = (array) array_pop($files);
$default_images[$image_target] = $file['uri'];
}
// Create an image field and add an instance to the post content type.
$field_name = strtolower($this->randomName());
$field_settings = array(
'default_image' => $default_images['field'],
);
$instance_settings = array(
'default_image' => $default_images['instance'],
);
$widget_settings = array(
'preview_image_style' => 'medium',
);
$this->createImageField($field_name, 'post', $field_settings, $instance_settings, $widget_settings);
$field = field_info_field($field_name);
$instance = field_info_instance('node', $field_name, 'post');
// Add another instance with another default image to the page content type.
$instance2 = array_merge($instance, array(
'bundle' => 'page',
'settings' => array(
'default_image' => $default_images['instance2'],
),
));
field_create_instance($instance2);
$instance2 = field_info_instance('node', $field_name, 'page');
// Confirm the defaults are present on the post field admin form.
$this->backdropGet("admin/structure/types/manage/post/fields/$field_name");
// Post instance settings.
$xpath = $this->xpath('//*[@id="edit-instance"]//div[contains(@class, "form-type-file")]//a[@href=:path]', array(':path' => file_create_url($default_images['instance'])));
$this->assertEqual(count($xpath), 1, format_string('Post image field instance default references expected file uri of @uri.', array('@uri' => $default_images['instance'])));
// Global field settings.
$xpath = $this->xpath('//*[@id="edit-field"]//div[contains(@class, "form-type-file")]//a[@href=:path]', array(':path' => file_create_url($default_images['field'])));
$this->assertEqual(count($xpath), 1, format_string('Post image field default references expected file uri of @uri.', array('@uri' => $default_images['field'])));
// Confirm the defaults are present on the page field admin form.
$this->backdropGet("admin/structure/types/manage/page/fields/$field_name");
// Page instance settings.
$xpath = $this->xpath('//*[@id="edit-instance"]//div[contains(@class, "form-type-file")]//a[@href=:path]', array(':path' => file_create_url($default_images['instance2'])));
$this->assertEqual(count($xpath), 1, format_string('Page image field instance default references expected file uri of @uri.', array('@uri' => $default_images['instance2'])));
// Global field settings.
$xpath = $this->xpath('//*[@id="edit-field"]//div[contains(@class, "form-type-file")]//a[@href=:path]', array(':path' => file_create_url($default_images['field'])));
$this->assertEqual(count($xpath), 1, format_string('Page image field default references expected file uri of @uri.', array('@uri' => $default_images['field'])));
// Confirm that the image default is shown for a new post node.
$post = $this->backdropCreateNode(array('type' => 'post'));
$post_built = node_view($post);
$this->assertEqual(
$post_built[$field_name]['#items'][0]['uri'],
$default_images['instance'],
format_string(
'A new post node without an image has the expected default image file URI of @uri.',
array('@uri' => $default_images['instance'])
)
);
// Confirm that the image default is shown for a new page node.
$page = $this->backdropCreateNode(array('type' => 'page'));
$page_built = node_view($page);
$this->assertEqual(
$page_built[$field_name]['#items'][0]['uri'],
$default_images['instance2'],
format_string(
'A new page node without an image has the expected default image file ID of @uri.',
array('@uri' => $default_images['instance2'])
)
);
// Upload a new default for the field.
$field['settings']['default_image'] = $default_images['field_new'];
field_update_field($field);
// Confirm that the new field default is used on the post admin form.
$this->backdropGet("admin/structure/types/manage/post/fields/$field_name");
$xpath = $this->xpath('//*[@id="edit-field"]//div[contains(@class, "form-type-file")]//a[@href=:path]', array(':path' => file_create_url($default_images['field_new'])));
$this->assertEqual(count($xpath), 1, format_string('Updated image field default references expected file uri of @uri.', array('@uri' => $default_images['field_new'])));
// Reload the nodes and confirm the field instance defaults are used.
$post_built = node_view($post = node_load($post->nid, NULL, $reset = TRUE));
$page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
$this->assertEqual(
$post_built[$field_name]['#items'][0]['uri'],
$default_images['instance'],
format_string(
'An existing post node without an image has the expected default image file URI of @uri.',
array('@uri' => $default_images['instance'])
)
);
$this->assertEqual(
$page_built[$field_name]['#items'][0]['uri'],
$default_images['instance2'],
format_string(
'An existing page node without an image has the expected default image file URI of @uri.',
array('@uri' => $default_images['instance2'])
)
);
// Upload a new default for the post's field instance.
$instance['settings']['default_image'] = $default_images['instance_new'];
field_update_instance($instance);
// Confirm the new field instance default is used on the post field
// admin form.
$this->backdropGet("admin/structure/types/manage/post/fields/$field_name");
$xpath = $this->xpath('//*[@id="edit-instance"]//div[contains(@class, "form-type-file")]//a[@href=:path]', array(':path' => file_create_url($default_images['instance_new'])));
$this->assertEqual(count($xpath), 1, format_string('Updated image field default references expected file uri of @uri.', array('@uri' => $default_images['instance_new'])));
// Reload the nodes.
$post_built = node_view($post = node_load($post->nid, NULL, $reset = TRUE));
$page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
// Confirm the post uses the new default.
$this->assertEqual(
$post_built[$field_name]['#items'][0]['uri'],
$default_images['instance_new'],
format_string(
'An existing post node without an image has the expected default image file URI of @uri.',
array('@uri' => $default_images['instance_new'])
)
);
// Confirm the page remains unchanged.
$this->assertEqual(
$page_built[$field_name]['#items'][0]['uri'],
$default_images['instance2'],
format_string(
'An existing page node without an image has the expected default image file URI of @uri.',
array('@uri' => $default_images['instance2'])
)
);
// Remove the instance default from posts.
$instance['settings']['default_image'] = NULL;
field_update_instance($instance);
// Confirm the post field instance default has been removed.
$this->backdropGet("admin/structure/types/manage/post/fields/$field_name");
$xpath = $this->xpath('//*[@id="edit-instance"]//div[contains(@class, "form-type-file")]//a[@href=:path]', array(':path' => file_create_url($default_images['instance_new'])));
$this->assertEqual(count($xpath), 0, format_string('Updated post image field instance default has been successfully removed.'));
// Reload the nodes.
$post_built = node_view($post = node_load($post->nid, NULL, $reset = TRUE));
$page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
// Confirm the post uses the new field (not instance) default.
$this->assertEqual(
$post_built[$field_name]['#items'][0]['uri'],
$default_images['field_new'],
format_string(
'An existing post node without an image has the expected default image file URI of @uri.',
array('@uri' => $default_images['field_new'])
)
);
// Confirm the page remains unchanged.
$this->assertEqual(
$page_built[$field_name]['#items'][0]['uri'],
$default_images['instance2'],
format_string(
'An existing page node without an image has the expected default image file URI of @uri.',
array('@uri' => $default_images['instance2'])
)
);
}