You are here

public function ImageWidgetCropTest::testImageWidgetCrop in Image Widget Crop 8.2

Test Image Widget Crop.

File

tests/src/FunctionalJavascript/ImageWidgetCropTest.php, line 97

Class

ImageWidgetCropTest
Minimal test case for the image_widget_crop module.

Namespace

Drupal\Tests\image_widget_crop\FunctionalJavascript

Code

public function testImageWidgetCrop() {

  // Test that crop widget works properly.
  $this
    ->createImageField('field_image_crop_test', 'crop_test', 'image_widget_crop', [], [], [
    'crop_list' => [
      'crop_16_9' => 'crop_16_9',
    ],
    'crop_types_required' => [],
  ]);
  $this
    ->drupalGetTestFiles('image');
  $this
    ->drupalGet('node/add/crop_test');

  // Assert that there is no crop widget, neither 'Alternative text' text
  // filed nor 'Remove' button yet.
  $assert_session = $this
    ->assertSession();
  $assert_session
    ->elementNotExists('css', 'summary:contains(Crop image)');
  $assert_session
    ->pageTextNotContains('Alternative text');
  $assert_session
    ->fieldNotExists('field_image_crop_test_0_remove_button');

  // Upload an image in field_image_crop_test_0.
  $this
    ->getSession()
    ->getPage()
    ->attachFileToField('files[field_image_crop_test_0]', $this->container
    ->get('file_system')
    ->realpath('public://image-test.jpg'));

  // Assert that now crop widget and 'Alternative text' text field appear and
  // that 'Remove' button exists.
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->elementExists('css', 'summary:contains(Crop image)');
  $assert_session
    ->pageTextContains('Alternative text');
  $assert_session
    ->buttonExists('Remove');

  // Set title and 'Alternative text' text field and save.
  $title = $this
    ->randomMachineName();
  $edit = [
    'title[0][value]' => $title,
    'field_image_crop_test[0][alt]' => $this
      ->randomMachineName(),
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $assert_session
    ->pageTextContains('Crop test ' . $title . ' has been created.');
  $url = $this
    ->getUrl();
  $nid = substr($url, -1, strrpos($url, '/'));

  // Edit crop image.
  $this
    ->drupalGet('node/' . $nid . '/edit');

  // Verify that the 'Remove' button works properly.
  $assert_session
    ->pageTextContains('Alternative text');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Remove');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->pageTextNotContains('Alternative text');
  $this
    ->getSession()
    ->getPage()
    ->attachFileToField('files[field_image_crop_test_0]', $this->container
    ->get('file_system')
    ->realpath('public://image-test.jpg'));

  // Verify that the 'Preview' button works properly.
  $this
    ->drupalPostForm(NULL, $edit, 'Preview');
  $assert_session
    ->linkExists('Back to content editing');
  $this
    ->clickLink('Back to content editing');

  // Verify that there is an image style preview.
  $assert_session
    ->hiddenFieldValueEquals('field_image_crop_test[0][width]', '40');
  $assert_session
    ->hiddenFieldValueEquals('field_image_crop_test[0][height]', '20');
}