You are here

TranslationOperationsFieldPermissionsTest.php in Translation Views 8

File

tests/src/Functional/TranslationOperationsFieldPermissionsTest.php
View source
<?php

namespace Drupal\Tests\translation_views\Functional;

use Drupal\Core\Entity\EntityStorageException;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\user\Entity\Role;
use Drupal\views\Tests\ViewTestData;

/**
 * Class TranslationOperationsFieldPermissionsTest.
 *
 * @group translation_views
 *
 * @package Drupal\Tests\translation_views\Functional
 */
class TranslationOperationsFieldPermissionsTest extends ViewTestBase {

  /**
   * List of the additional language IDs to be created for the tests.
   *
   * @var array
   */
  private static $langcodes = [
    'fr',
    'de',
    'it',
    'af',
    'sq',
  ];

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'content_translation',
    'node',
    'translation_views',
    'translation_views_test_views',
  ];

  /**
   * {@inheritdoc}
   */
  protected $profile = 'standard';

  /**
   * Testing views ID array.
   *
   * @var array
   */
  public static $testViews = [
    'test_operations_links',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);
    $this
      ->drupalLogin($this->rootUser);

    // Set up testing views.
    ViewTestData::createTestViews(get_class($this), [
      'translation_views_test_views',
    ]);
    try {
      $this
        ->setUpLanguages();
    } catch (EntityStorageException $e) {
      $this
        ->verbose($e
        ->getMessage());
    }

    // Enable translation for Article nodes.
    $this
      ->enableTranslation('node', 'article');

    // Create testing node.
    $this
      ->drupalPostForm('node/add/article', [
      'title[0][value]' => $this
        ->randomString(),
    ], 'Save');
    $this
      ->drupalLogout();
  }

  /**
   * Set up languages.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  private function setUpLanguages() {
    foreach (self::$langcodes as $langcode) {
      ConfigurableLanguage::createFromLangcode($langcode)
        ->save();
    }
  }

  /**
   * Change language settings for entity types.
   *
   * @param string $category
   *   Entity category (e.g. node).
   * @param string $subcategory
   *   Entity subcategory (e.g. article).
   */
  private function enableTranslation($category, $subcategory) {
    $this
      ->drupalPostForm('admin/config/regional/content-language', [
      "entity_types[{$category}]" => 1,
      "settings[{$category}][{$subcategory}][translatable]" => 1,
      "settings[{$category}][{$subcategory}][settings][language][language_alterable]" => 1,
    ], 'Save configuration');
    \Drupal::entityTypeManager()
      ->clearCachedDefinitions();
  }

  /**
   * Translate node all specified languages.
   */
  private function translateNode() {
    $node = Node::load(1);
    foreach (self::$langcodes as $langcode) {
      if (!$node
        ->hasTranslation($langcode)) {
        $node
          ->addTranslation($langcode, [
          'title' => $this
            ->randomMachineName(),
        ])
          ->save();
      }
    }
  }

  /**
   * Add specific set of permissions to the "authenticated" role.
   *
   * @param array $permissions
   *   Permissions array.
   */
  private function addPermissionsForAuthUser(array $permissions = []) {
    if (!empty($permissions)) {
      $role = Role::load(Role::AUTHENTICATED_ID);
      if ($role instanceof Role) {
        $this
          ->grantPermissions($role, $permissions);
      }
    }
  }

  /**
   * Test translation operation create permissions.
   *
   * @throws \Behat\Mink\Exception\ElementTextException
   * @throws \Behat\Mink\Exception\ExpectationException
   */
  public function testTranslationOperationsCreatePermissions() {
    $default_language = \Drupal::languageManager()
      ->getDefaultLanguage();
    $target_language = static::$langcodes[mt_rand(0, 4)];
    $this
      ->assertNotNull($target_language);
    $this
      ->assertNotNull($default_language);
    $userCreate = $this
      ->createUser([
      'create content translations',
    ]);
    $this
      ->drupalLogin($userCreate);
    $this
      ->drupalGet('/translate/content', [
      'query' => [
        'langcode' => $default_language
          ->getId(),
        'translation_target_language' => $target_language,
        'translation_outdated' => 'All',
        'translation_status' => 'All',
      ],
    ]);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertSession()
      ->elementNotExists('css', 'table > tbody > tr:nth-child(1) .views-field-translation-operations ul li a');
    $this
      ->addPermissionsForAuthUser([
      'translate any entity',
    ]);
    $this
      ->assertTrue($userCreate
      ->hasPermission('translate any entity'));
    $this
      ->drupalGet('/translate/content', [
      'query' => [
        'langcode' => $default_language
          ->getId(),
        'translation_target_language' => $target_language,
        'translation_outdated' => 'All',
        'translation_status' => 'All',
      ],
    ]);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertSession()
      ->elementTextContains('css', "table > tbody > tr:nth-child(1) .views-field-translation-operations ul li a", 'Add');
  }

  /**
   * Test translation operation update permissions.
   *
   * @throws \Behat\Mink\Exception\ElementTextException
   * @throws \Behat\Mink\Exception\ExpectationException
   */
  public function testTranslationOperationsUpdatePermissions() {
    $this
      ->translateNode();
    $userUpdate = $this
      ->createUser([
      'update content translations',
    ]);
    $this
      ->drupalLogin($userUpdate);

    // Check without translation permission.
    $this
      ->drupalGet('/test_operations_links', [
      'query' => [
        'translation_target_language' => 'fr',
      ],
    ]);
    $this
      ->assertSession()
      ->elementTextNotContains('css', ".view-content > div:nth-child(1) .views-field-translation-operations", 'Edit');

    // Check with translation permission.
    $this
      ->addPermissionsForAuthUser([
      'translate article node',
    ]);
    $this
      ->assertTrue($userUpdate
      ->hasPermission('translate article node'));
    $this
      ->drupalGet('/test_operations_links', [
      'query' => [
        'translation_target_language' => 'fr',
      ],
    ]);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertSession()
      ->elementTextContains('css', '.view-content > div:nth-child(1) .views-field-translation-operations ul .edit a', 'Edit');
    $this
      ->click('.view-content > div:nth-child(1) .views-field-translation-operations ul .edit a');
    $this
      ->assertUrl('/fr/node/1/translations/edit/fr');

    // Check with edit permission.
    $this
      ->addPermissionsForAuthUser([
      'edit any article content',
    ]);
    $this
      ->assertTrue($userUpdate
      ->hasPermission('edit any article content'));
    $this
      ->drupalGet('/test_operations_links', [
      'query' => [
        'translation_target_language' => 'fr',
      ],
    ]);
    $this
      ->assertSession()
      ->elementTextContains('css', ".view-content > div:nth-child(1) .views-field-translation-operations ul .edit a", 'Edit');
    $this
      ->click('.view-content > div:nth-child(1) .views-field-translation-operations ul .edit a');
    $this
      ->assertUrl('/fr/node/1/edit');
  }

  /**
   * Test translation operation delete permissions.
   *
   * @throws \Behat\Mink\Exception\ElementTextException
   * @throws \Behat\Mink\Exception\ExpectationException
   */
  public function testTranslationOperationsDeletePermissions() {
    $this
      ->translateNode();
    $userDelete = $this
      ->createUser([
      'delete content translations',
    ]);
    $this
      ->drupalLogin($userDelete);

    // Check without translation permission.
    $this
      ->drupalGet('/test_operations_links', [
      'query' => [
        'translation_target_language' => 'fr',
      ],
    ]);
    $this
      ->assertSession()
      ->elementTextNotContains('css', ".view-content > div:nth-child(1) .views-field-translation-operations", 'Delete');

    // Check with translation permission.
    $this
      ->addPermissionsForAuthUser([
      'translate article node',
    ]);
    $this
      ->assertTrue($userDelete
      ->hasPermission('translate article node'));
    $this
      ->drupalGet('/test_operations_links', [
      'query' => [
        'translation_target_language' => 'fr',
      ],
    ]);
    $this
      ->assertSession()
      ->elementTextContains('css', ".view-content > div:nth-child(1) .views-field-translation-operations ul .delete a", 'Delete');
    $this
      ->click('.view-content > div:nth-child(1) .views-field-translation-operations ul .delete a');
    $this
      ->assertUrl('/fr/node/1/translations/delete/fr');

    // Check with edit permission.
    $this
      ->addPermissionsForAuthUser([
      'delete any article content',
    ]);
    $this
      ->assertTrue($userDelete
      ->hasPermission('delete any article content'));
    $this
      ->drupalGet('/test_operations_links', [
      'query' => [
        'translation_target_language' => 'fr',
      ],
    ]);
    $this
      ->assertSession()
      ->elementTextContains('css', ".view-content > div:nth-child(1) .views-field-translation-operations ul .delete a", 'Delete');
    $this
      ->click('.view-content > div:nth-child(1) .views-field-translation-operations ul .delete a');
    $this
      ->assertUrl('/fr/node/1/delete');
  }

}

Classes

Namesort descending Description
TranslationOperationsFieldPermissionsTest Class TranslationOperationsFieldPermissionsTest.