View source
<?php
namespace Drupal\Tests\comment_alter\Functional;
use Drupal\Tests\comment_alter\Functional\CommentAlterTestBase;
class CommentAlterImageTest extends CommentAlterTestBase {
public static $modules = [
'image',
];
protected function addImageField($cardinality) {
return $this
->addField('image', 'image_image', [
'cardinality' => $cardinality,
]);
}
protected function getImageFiles() {
$original = drupal_get_path('module', 'simpletest') . '/files';
$images = file_scan_directory($original, '/image-.*/');
foreach ($images as $image) {
file_unmanaged_copy($image->uri, \Drupal\Core\StreamWrapper\PublicStream::basePath());
}
return $images;
}
protected function postCommentWithImage($field_name, $img_field, $field_number = 0) {
$this
->drupalGet('comment/reply/' . $this->entityType . '/' . $this->entity
->id() . '/comment');
$this
->drupalPostForm(NULL, $img_field, t('Upload'));
$edit['comment_alter_fields[' . $field_name . '][' . $field_number . '][alt]'] = $this
->randomString();
$edit['comment_body[0][value]'] = $this
->randomString();
$edit['subject[0][value]'] = $this
->randomString();
$this
->drupalPostForm(NULL, $edit, t('Save'));
}
public function testImageFieldSingle() {
$field_name = $this
->addImageField(1);
$this
->createEntityObject();
$image = current($this
->getImageFiles());
$this
->postCommentWithImage($field_name, [
'files[comment_alter_fields_' . $field_name . '_0]' => drupal_realpath($image->uri),
]);
$this
->assertCommentDiff([
$field_name => [
[
NULL,
'Image: ' . $image->filename,
],
[
NULL,
'File ID: 1',
],
],
]);
$this
->assertRevisionDelete();
}
public function testImageFieldMultiple() {
$field_name = $this
->addImageField(-1);
$images = $this
->getImageFiles();
$image1 = current($this
->getImageFiles());
$image2 = end($images);
$this
->createEntityObject();
$this
->drupalGet('entity_test_rev/manage/' . $this->entity
->id() . '/edit');
$img['files[' . $field_name . '_0][]'] = drupal_realpath($image1->uri);
$this
->drupalPostForm(NULL, $img, t('Upload'));
$edit[$field_name . '[0][alt]'] = $this
->randomString();
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->postCommentWithImage($field_name, [
'files[comment_alter_fields_' . $field_name . '_1][]' => drupal_realpath($image2->uri),
], 1);
$this
->assertCommentDiff([
$field_name => [
[
'File ID: 1',
'File ID: 1',
],
[
NULL,
'Image: ' . $image2->filename,
],
[
NULL,
'File ID: 2',
],
],
]);
$this
->assertRevisionDelete();
}
}