public function AdminTest::testFieldEditUi in Image Replace 7
Same name and namespace in other branches
- 8 src/Tests/AdminTest.php \Drupal\image_replace\Tests\AdminTest::testFieldEditUi()
Tests image replacement on node entities.
File
- src/
Tests/ AdminTest.php, line 85
Class
- AdminTest
- Tests administrative interface for image replace.
Namespace
Drupal\image_replace\TestsCode
public function testFieldEditUi() {
list($original_file, $replacement_file) = $this
->createTestFiles();
// Create an unrelated image style.
$unrelated_style_name = 'other_style';
$style = image_style_save(array(
'name' => $unrelated_style_name,
'label' => $this
->randomString(),
));
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/structure/types/manage/article/fields/image_original');
// Verify that a select field is present with a list of available source
// fields for the generated image style.
$field_name = 'instance[settings][image_replace][' . $this->styleName . '][source_field]';
$this
->assertFieldByName($field_name, "0", 'Image replace selector found for style containing the image replace effect');
$result = $this
->xpath($this
->constructFieldXpath('name', $field_name));
$options = $this
->getAllOptions($result[0]);
$contains_image_original = FALSE;
$contains_image_replacement = FALSE;
foreach ($options as $option) {
$contains_image_original |= $option['value'] == 'image_original';
$contains_image_replacement |= $option['value'] == 'image_replacement';
}
$this
->assertFalse($contains_image_original, 'Original image field is not in the list of options');
$this
->assertTrue($contains_image_replacement, 'Replacement image field is in the list of options');
// Verify that no select field is present for an image style which does not
// contain the replacement effect.
$field_name = 'instance[settings][image_replace][' . $unrelated_style_name . '][source_field]';
$this
->assertNoFieldByName($field_name, NULL, 'Image replace settings not present for untrelated style');
// Choose the replacement image field as the replacement source.
$field_name = 'instance[settings][image_replace][' . $this->styleName . '][source_field]';
$edit = array(
$field_name => 'image_replacement',
);
$this
->drupalPost(NULL, $edit, t('Save settings'));
// Verify that no message is displayed if the mapping changes when there is
// no existing content.
$this
->assertNoText('The image replacement settings have been modified. As a result, it is necessary to rebuild the image replacement mapping for existing content. Note: The replacement mapping is updated automatically when saving an entity.');
// Post new content.
$edit = array(
'title' => $this
->randomName(),
'promote' => 1,
);
$edit['files[image_original_' . LANGUAGE_NONE . '_0]'] = drupal_realpath($original_file->uri);
$edit['files[image_replacement_' . LANGUAGE_NONE . '_0]'] = drupal_realpath($replacement_file->uri);
$this
->drupalPost('node/add/article', $edit, t('Save'));
$this
->assertResponse(200);
preg_match('/node\\/([0-9]+)/', $this
->getUrl(), $matches);
$node = node_load($matches[1]);
// Verify that the original image is shown on the full node view.
$generated_url = file_create_url($node->image_original[LANGUAGE_NONE][0]['uri']);
$this
->assertRaw(check_plain($generated_url), 'Original image displayed');
$generated_image_data = $this
->drupalGet($generated_url);
$this
->assertResponse(200);
// Assert that the result is the original image.
$generated_uri = file_unmanaged_save_data($generated_image_data);
$this
->assertTrue($this
->imageIsOriginal($generated_uri), 'The generated file should be the same as the original file on full node view.');
// Verify that the replacement image is shown on the teaser.
$this
->drupalGet('node');
$generated_url = image_style_url($this->styleName, $node->image_original[LANGUAGE_NONE][0]['uri']);
$this
->assertRaw(check_plain($generated_url), format_string('Image displayed using style @style.', array(
'@style' => $this->styleName,
)));
$generated_image_data = $this
->drupalGet($generated_url);
$this
->assertResponse(200);
// Assert that the result is the replacement image.
$generated_uri = file_unmanaged_save_data($generated_image_data);
$this
->assertTrue($this
->imageIsReplacement($generated_uri), 'The generated file should be the same as the replacement file on teaser.');
// Go back to the field settings and reset the replacement mapping.
$field_name = 'instance[settings][image_replace][' . $this->styleName . '][source_field]';
$edit = array(
$field_name => '0',
);
$this
->drupalPost('admin/structure/types/manage/article/fields/image_original', $edit, t('Save settings'));
// Verify that a message is displayed if the mapping changes when there is
// existing content.
$this
->assertText('The image replacement settings have been modified. As a result, it is necessary to rebuild the image replacement mapping for existing content. Note: The replacement mapping is updated automatically when saving an entity.');
}