View source
<?php
namespace Drupal\Tests\thunder\FunctionalJavascript;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\media\Entity\Media;
use Imagick;
class MediaImageModifyTest extends ThunderJavascriptTestBase {
use ThunderEntityBrowserTestTrait;
use ThunderParagraphsTestTrait;
public function testFocalPointChange() {
$mediaId = 9;
$this
->drupalGet("media/{$mediaId}/edit");
$this
->createScreenshot($this
->getScreenshotFolder() . '/MediaImageModifyTest_BeforeFocalPointChange_' . date('Ymd_His') . '.png');
$this
->getSession()
->getDriver()
->executeScript('var e = new jQuery.Event("click"); e.offsetX = 48; e.offsetY = 15; jQuery(".focal-point-wrapper img").trigger(e);');
$this
->createScreenshot($this
->getScreenshotFolder() . '/MediaImageModifyTest_AfterFocalPointChange_' . date('Ymd_His') . '.png');
$this
->clickSave();
$media = Media::load($mediaId);
$img = $media
->get('field_image')->target_id;
$file = File::load($img);
$path = $file
->getFileUri();
$derivativeUri = ImageStyle::load('teaser')
->buildUri($path);
ImageStyle::load('teaser')
->createDerivative($path, $derivativeUri);
$image1 = new Imagick($derivativeUri);
$image2 = new Imagick(realpath(dirname(__FILE__) . '/../../fixtures/reference.jpg'));
$result = $image1
->compareImages($image2, \Imagick::METRIC_MEANSQUAREERROR);
$this
->assertTrue($result[1] < 0.01, 'Images are identical');
$image1
->clear();
$image2
->clear();
}
public function testImageEdit() {
$mediaId = 9;
$page = $this
->getSession()
->getPage();
$this
->drupalGet("media/{$mediaId}/edit");
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->fillField('name[0][value]', "Media {$mediaId}");
$page
->fillField('field_image[0][alt]', "Media {$mediaId} Alt Text");
$page
->fillField('field_image[0][title]', "Media {$mediaId} Title");
$this
->setRawFieldValue('field_expires[0][value][date]', '2022-12-18');
$this
->setRawFieldValue('field_expires[0][value][time]', '01:02:03');
$page
->fillField('field_copyright[0][value]', "Media {$mediaId} Copyright");
$page
->fillField('field_source[0][value]', "Media {$mediaId} Source");
$this
->fillCkEditor('#edit-field-description-0-value', "Media {$mediaId} Description");
$this
->createScreenshot($this
->getScreenshotFolder() . '/MediaImageModifyTest_BeforeImageEditSave_' . date('Ymd_His') . '.png');
$this
->clickSave();
$this
->drupalGet("media/{$mediaId}/edit");
$this
->createScreenshot($this
->getScreenshotFolder() . '/MediaImageModifyTest_AfterImageEdit_' . date('Ymd_His') . '.png');
$this
->assertSession()
->fieldValueEquals('name[0][value]', "Media {$mediaId}");
$this
->assertSession()
->fieldValueEquals('field_image[0][alt]', "Media {$mediaId} Alt Text");
$this
->assertSession()
->fieldValueEquals('field_image[0][title]', "Media {$mediaId} Title");
$this
->assertSession()
->fieldValueEquals('field_expires[0][value][date]', '2022-12-18');
$this
->assertSession()
->fieldValueEquals('field_expires[0][value][time]', '01:02:03');
$this
->assertSession()
->fieldValueEquals('field_copyright[0][value]', "Media {$mediaId} Copyright");
$this
->assertSession()
->fieldValueEquals('field_source[0][value]', "Media {$mediaId} Source");
$this
->assertSession()
->fieldValueEquals('field_description[0][value]', "<p>Media {$mediaId} Description</p>");
}
public function testRemoveAdd() {
$this
->drupalGet("node/6/edit");
$page = $this
->getSession()
->getPage();
$this
->editParagraph($page, 'field_paragraphs', 0);
$this
->clickAjaxButtonCssSelector('[data-drupal-selector="edit-field-paragraphs-0-subform-field-image-current-items-0-remove-button"]');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->elementNotExists('css', '[data-drupal-selector="edit-field-paragraphs-0-subform-field-image-wrapper"] div.messages--error');
$this
->openEntityBrowser($page, 'edit-field-paragraphs-0-subform-field-image-entity-browser-entity-browser-open-modal', 'image_browser');
$this
->clickButtonCssSelector($page, '#entity-browser-image-browser-form div.view-content > div.views-row:nth-child(1)');
$fileName = $this
->getSession()
->evaluateScript('jQuery(\'#entity-browser-image-browser-form div.view-content > div.views-row:nth-child(1) img\').attr(\'src\').split(\'?\')[0].split(\'/\').splice(-1);');
$this
->clickButtonDrupalSelector($page, 'edit-submit');
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->clickAjaxButtonCssSelector('[name="field_paragraphs_0_collapse"]');
$this
->assertEquals($fileName, $this
->getSession()
->evaluateScript('jQuery(\'[data-drupal-selector="edit-field-paragraphs-0-preview"] div.paragraph-preview__thumbnail img\').attr(\'src\').split(\'?\')[0].split(\'/\').splice(-1)'), 'Image file should be identical to previously selected.');
}
}