You are here

public function CropFunctionalTest::testCropTypeCrud in Crop API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/CropFunctionalTest.php \Drupal\Tests\crop\Functional\CropFunctionalTest::testCropTypeCrud()

Tests crop type crud pages.

File

tests/src/Functional/CropFunctionalTest.php, line 66

Class

CropFunctionalTest
Functional tests for crop API.

Namespace

Drupal\Tests\crop\Functional

Code

public function testCropTypeCrud() {

  // Anonymous users don't have access to crop type admin pages.
  $this
    ->drupalGet('admin/config/media/crop');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->drupalGet('admin/config/media/crop/add');
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Can access pages if logged in and no crop types exist.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('admin/config/media/crop');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains(t('No crop types available.'));
  $this
    ->assertSession()
    ->linkExists(t('Add crop type'));

  // Can access add crop type form.
  $this
    ->clickLink(t('Add crop type'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->addressEquals('admin/config/media/crop/add');

  // Create crop type.
  $crop_type_id = strtolower($this
    ->randomMachineName());
  $edit = [
    'id' => $crop_type_id,
    'label' => $this
      ->randomMachineName(),
    'description' => $this->randomGenerator
      ->sentences(10),
  ];
  $this
    ->drupalPostForm('admin/config/media/crop/add', $edit, t('Save crop type'));
  $this
    ->assertSession()
    ->responseContains(t('The crop type %name has been added.', [
    '%name' => $edit['label'],
  ]));
  $this->cropType = CropType::load($crop_type_id);
  $this
    ->assertSession()
    ->addressEquals('admin/config/media/crop');
  $label = $this
    ->xpath("//td[contains(concat(' ',normalize-space(@class),' '),' menu-label ')]");
  self::assertTrue(strpos($label[0]
    ->getText(), $edit['label']) !== FALSE, 'Crop type label found on listing page.');
  $this
    ->assertSession()
    ->pageTextContains($edit['description']);

  // Check edit form.
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertSession()
    ->pageTextContains(t('Edit @name crop type', [
    '@name' => $edit['label'],
  ]));
  $this
    ->assertSession()
    ->responseContains($edit['id']);
  $this
    ->assertSession()
    ->fieldExists('edit-label');
  $this
    ->assertSession()
    ->responseContains($edit['description']);

  // See if crop type appears on image effect configuration form.
  $this
    ->drupalGet('admin/config/media/image-styles/manage/' . $this->testStyle
    ->id() . '/add/crop_crop');
  $option = $this
    ->xpath("//select[@id='edit-data-crop-type']/option");
  self::assertTrue(strpos($option[0]
    ->getText(), $edit['label']) !== FALSE, 'Crop type label found on image effect page.');
  $this
    ->drupalPostForm('admin/config/media/image-styles/manage/' . $this->testStyle
    ->id() . '/add/crop_crop', [
    'data[crop_type]' => $edit['id'],
  ], t('Add effect'));
  $this
    ->assertSession()
    ->pageTextContains(t('The image effect was successfully applied.'));
  $this
    ->assertSession()
    ->pageTextContains(t('Manual crop uses @name crop type', [
    '@name' => $edit['label'],
  ]));
  $this->testStyle = $this->container
    ->get('entity_type.manager')
    ->getStorage('image_style')
    ->loadUnchanged($this->testStyle
    ->id());
  self::assertEquals($this->testStyle
    ->getEffects()
    ->count(), 1, 'One image effect added to test image style.');
  $effect_configuration = $this->testStyle
    ->getEffects()
    ->getIterator()
    ->current()
    ->getConfiguration();
  self::assertEquals($effect_configuration['data'], [
    'crop_type' => $edit['id'],
  ], 'Manual crop effect uses correct image style.');

  // Tests the image URI is extended with shortened hash in case of image
  // style and corresponding crop existence.
  $this
    ->doTestFileUriAlter();

  // Try to access edit form as anonymous user.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('admin/config/media/crop/manage/' . $edit['id']);
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->drupalLogin($this->adminUser);

  // Try to create crop type with same machine name.
  $this
    ->drupalPostForm('admin/config/media/crop/add', $edit, t('Save crop type'));
  $this
    ->assertSession()
    ->pageTextContains(t('The machine-readable name is already in use. It must be unique.'));

  // Delete crop type.
  $this
    ->drupalGet('admin/config/media/crop');
  $this
    ->assertSession()
    ->linkExists('Test image style');
  $this
    ->clickLink(t('Delete'));
  $this
    ->assertSession()
    ->pageTextContains(t('Are you sure you want to delete the crop type @name?', [
    '@name' => $edit['label'],
  ]));
  $this
    ->drupalPostForm('admin/config/media/crop/manage/' . $edit['id'] . '/delete', [], t('Delete'));
  $this
    ->assertSession()
    ->responseContains(t('The crop type %name has been deleted.', [
    '%name' => $edit['label'],
  ]));
  $this
    ->assertSession()
    ->pageTextContains(t('No crop types available.'));
}