You are here

public function AddPhotosAlbumImageTest::testAccessPhotosImageEditForm in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 tests/src/Functional/AddPhotosAlbumImageTest.php \Drupal\Tests\photos\Functional\AddPhotosAlbumImageTest::testAccessPhotosImageEditForm()
  2. 8.4 tests/src/Functional/AddPhotosAlbumImageTest.php \Drupal\Tests\photos\Functional\AddPhotosAlbumImageTest::testAccessPhotosImageEditForm()

Test adding an image to an album and accessing the image edit page.

File

tests/src/Functional/AddPhotosAlbumImageTest.php, line 74

Class

AddPhotosAlbumImageTest
Test creating a new album, adding an image and updating the image.

Namespace

Drupal\Tests\photos\Functional

Code

public function testAccessPhotosImageEditForm() {

  // Create a test album node.
  $albumTitle = $this
    ->randomMachineName();
  $album = Node::create([
    'type' => 'photos',
    'title' => $albumTitle,
  ]);
  $album
    ->save();

  // Get test image file.
  $testPhotoUri = drupal_get_path('module', 'photos') . '/tests/images/photos-test-picture.jpg';
  $fileSystem = \Drupal::service('file_system');

  // Post image upload form.
  $edit = [
    'files[images_0]' => $fileSystem
      ->realpath($testPhotoUri),
    'title_0' => 'Test photo title',
    'des_0' => 'Test photos description',
  ];
  $this
    ->drupalGet('node/' . $album
    ->id() . '/photos');
  $this
    ->submitForm($edit, 'Confirm upload');

  // Get album images.
  $photosAlbum = new PhotosAlbum($album
    ->id());
  $albumImages = $photosAlbum
    ->getImages(1);
  $photosImage = $albumImages[0]['photos_image'];
  $this
    ->assertEquals($edit['title_0'], $photosImage
    ->getTitle());

  // Access image edit page.
  $this
    ->drupalGet('photos/' . $photosImage
    ->getAlbumId() . '/' . $photosImage
    ->id() . '/edit');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Post image edit form.
  $edit = [
    'title[0][value]' => 'Test new title',
  ];
  $this
    ->submitForm($edit, 'Save');

  // Confirm that image title has been updated.

  /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('photos_image');

  // Must explicitly clear cache to see new title.
  // @see https://www.drupal.org/project/drupal/issues/3040878
  $storage
    ->resetCache([
    $photosImage
      ->id(),
  ]);
  $photosImage = $storage
    ->load($photosImage
    ->id());
  $this
    ->assertEquals($edit['title[0][value]'], $photosImage
    ->getTitle());

  // Test recent albums content overview.
  $this
    ->drupalGet('photos');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->responseContains($albumTitle);

  // Check image file directory settings.
  $this
    ->assertStringContainsString('photos/images', $photosImage->field_image->entity
    ->getFileUri());

  // Change image field file directory settings.
  $this
    ->drupalLogin($this->adminAccount);
  $edit = [
    'settings[file_directory]' => 'custom_directory/images',
  ];
  $this
    ->drupalGet('admin/structure/photos/fields/photos_image.photos_image.field_image');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->submitForm($edit, 'Save settings');

  // Upload new image.
  $this
    ->drupalLogin($this->account);
  $edit = [
    'files[images_0]' => $fileSystem
      ->realpath($testPhotoUri),
    'title_0' => 'Test2 photo title',
    'des_0' => 'Test2 photos description',
  ];
  $this
    ->drupalGet('node/' . $album
    ->id() . '/photos');
  $this
    ->submitForm($edit, 'Confirm upload');

  // Get album images.
  $photosAlbum = new PhotosAlbum($album
    ->id());
  $albumImages = $photosAlbum
    ->getImages(2);
  $photosImage = $albumImages[1]['photos_image'];
  $this
    ->assertStringContainsString('custom_directory/images', $photosImage->field_image->entity
    ->getFileUri());
}