View source
<?php
namespace Drupal\Tests\photos\Functional;
use Drupal\node\Entity\Node;
use Drupal\photos\PhotosAlbum;
use Drupal\Tests\BrowserTestBase;
class AddPhotosAlbumImageTest extends BrowserTestBase {
protected static $modules = [
'node',
'field',
'field_ui',
'file',
'image',
'comment',
'photos',
'views',
];
protected $defaultTheme = 'stark';
protected $account;
protected $adminAccount;
protected function setUp() {
parent::setUp();
$this->account = $this
->drupalCreateUser([
'view photo',
'create photo',
'edit own photo',
'delete own photo',
]);
$this
->drupalLogin($this->account);
$this->adminAccount = $this
->drupalCreateUser([
'administer photos_image fields',
]);
}
public function testAccessPhotosImageEditForm() {
$albumTitle = $this
->randomMachineName();
$album = Node::create([
'type' => 'photos',
'title' => $albumTitle,
]);
$album
->save();
$testPhotoUri = drupal_get_path('module', 'photos') . '/tests/images/photos-test-picture.jpg';
$fileSystem = \Drupal::service('file_system');
$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');
$photosAlbum = new PhotosAlbum($album
->id());
$albumImages = $photosAlbum
->getImages(1);
$photosImage = $albumImages[0]['photos_image'];
$this
->assertEquals($edit['title_0'], $photosImage
->getTitle());
$this
->drupalGet('photos/' . $photosImage
->getAlbumId() . '/' . $photosImage
->id() . '/edit');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'title[0][value]' => 'Test new title',
];
$this
->submitForm($edit, 'Save');
$storage = $this->container
->get('entity_type.manager')
->getStorage('photos_image');
$storage
->resetCache([
$photosImage
->id(),
]);
$photosImage = $storage
->load($photosImage
->id());
$this
->assertEquals($edit['title[0][value]'], $photosImage
->getTitle());
$this
->drupalGet('photos');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains($albumTitle);
$this
->assertStringContainsString('photos/images', $photosImage->field_image->entity
->getFileUri());
$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');
$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');
$photosAlbum = new PhotosAlbum($album
->id());
$albumImages = $photosAlbum
->getImages(2);
$photosImage = $albumImages[1]['photos_image'];
$this
->assertStringContainsString('custom_directory/images', $photosImage->field_image->entity
->getFileUri());
}
}