You are here

protected function PhotosAccessTest::setUp in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 photos_access/tests/src/Functional/PhotosAccessTest.php \Drupal\Tests\photos\Functional\PhotosAccessTest::setUp()

Overrides BrowserTestBase::setUp

File

photos_access/tests/src/Functional/PhotosAccessTest.php, line 69

Class

PhotosAccessTest
Test photos_access album privacy settings.

Namespace

Drupal\Tests\photos\Functional

Code

protected function setUp() {
  parent::setUp();

  // Create admin user and adjust photos admin settings. This user will also
  // be the album owner.
  $this->account = $this
    ->drupalCreateUser([
    'access administration pages',
    'access content',
    'administer display modes',
    'administer nodes',
    'administer site configuration',
    'administer views',
    'create photo',
    'create photos content',
    'delete own photo',
    'edit own photo',
    'edit own photos content',
    'view photo',
  ]);
  $this
    ->drupalLogin($this->account);

  // Enable clean image titles and privacy settings.
  $edit = [
    'photos_access_photos' => 1,
    'photos_clean_title' => TRUE,
  ];

  // @todo more file upload and path tests.
  $this
    ->drupalGet('/admin/config/media/photos');
  $this
    ->submitForm($edit, 'Save configuration');

  // Edit views settings.
  $edit = [
    'access[type]' => 'photos_access',
  ];
  $this
    ->drupalGet('/admin/structure/views/nojs/display/photos_test_view/page_1/access');
  $this
    ->submitForm($edit, 'Apply');

  // Save photos_album view.
  $this
    ->submitForm([], 'Save');

  // Rebuild permissions.
  node_access_rebuild();

  // Create user for access denied tests.
  $this->accountViewPhotosOnly = $this
    ->drupalCreateUser([
    'access content',
    'view photo',
  ]);

  // Create user for role access test.
  $this
    ->drupalCreateRole([
    'access content',
    'view photo',
    'edit own photo',
  ], 'role_access_test', '<em>role_access_test</em>');
  $this->accountEditOwnPhotosRole = $this
    ->drupalCreateUser([]);
  $this->accountEditOwnPhotosRole
    ->addRole('role_access_test');
  $this->accountEditOwnPhotosRole
    ->save();

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

  // Create a locked photos node.
  $this
    ->drupalGet('/node/add/photos');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $edit = [
    'title[0][value]' => $this
      ->randomMachineName(),
    'photos_privacy[viewid]' => 1,
  ];
  $this
    ->submitForm($edit, 'Save');
  $storage
    ->resetCache([
    1,
  ]);

  /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $this->album = $storage
    ->load(1);
  $this
    ->assertNotNull($this->album->photos_privacy);
  $this
    ->assertEquals($this->album->photos_privacy['viewid'], 1, 'Album is set to locked.');

  // Get test image file.

  /** @var \Drupal\Core\File\FileSystemInterface $fileSystem */
  $fileSystem = \Drupal::service('file_system');
  $testImageFile = drupal_get_path('module', 'photos') . '/tests/images/photos-test-picture.jpg';

  // Add image to album.
  $edit = [
    'files[images_0]' => $fileSystem
      ->realpath($testImageFile),
  ];
  $this
    ->drupalGet('node/' . $this->album
    ->id() . '/photos');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->submitForm($edit, 'Confirm upload');
}