View source  
  <?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\user\Entity\Role;
use Drupal\Core\Url;
class LingotekManageLingotekTranslationsPermissionTest extends LingotekTestBase {
  
  public static $modules = [
    'lingotek',
    'lingotek_test',
    'node',
    'toolbar',
    'block',
  ];
  
  protected function setUp() : void {
    parent::setUp();
    $roles = $this->translationManagerUser
      ->getRoles(TRUE);
    
    $role = Role::load($roles[0]);
    $role
      ->grantPermission('access toolbar')
      ->save();
    
    $this
      ->drupalPlaceBlock('page_title_block', [
      'region' => 'content',
      'weight' => -5,
    ]);
    $this
      ->drupalPlaceBlock('local_tasks_block', [
      'region' => 'content',
      'weight' => -10,
    ]);
    
    $this
      ->drupalCreateContentType([
      'type' => 'article',
      'name' => 'Article',
    ]);
    
    ConfigurableLanguage::createFromLangcode('es')
      ->setThirdPartySetting('lingotek', 'locale', 'es_ES')
      ->save();
    
    ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
      ->setLanguageAlterable(TRUE)
      ->save();
    \Drupal::service('content_translation.manager')
      ->setEnabled('node', 'article', TRUE);
    drupal_static_reset();
    \Drupal::entityTypeManager()
      ->clearCachedDefinitions();
    $this
      ->applyEntityUpdates();
    
    $this
      ->rebuildContainer();
    $this
      ->saveLingotekContentTranslationSettingsForNodeTypes();
  }
  
  public function testCannotSeeSettingsTabWithoutRightPermission() {
    $assert_session = $this
      ->assertSession();
    $user = $this
      ->drupalCreateUser([
      'administer lingotek',
      'assign lingotek translation profiles',
      'manage lingotek translations',
    ]);
    
    $this
      ->drupalLogin($user);
    
    $this
      ->drupalGet('admin/lingotek/settings');
    
    $this
      ->assertNoText('You are not authorized to access this page.');
    $user = $this
      ->drupalCreateUser([
      'assign lingotek translation profiles',
      'manage lingotek translations',
    ]);
    
    $this
      ->drupalLogin($user);
    
    $this
      ->drupalGet('admin/lingotek/settings');
    
    $this
      ->assertText('You are not authorized to access this page.');
  }
  
  public function testNavigationThroughSiteForBulkContentTranslationAsTranslationsManager() {
    $assert_session = $this
      ->assertSession();
    
    $this
      ->drupalLogin($this->translationManagerUser);
    $this
      ->drupalGet('/user');
    
    $assert_session
      ->linkExists('Configuration');
    $assert_session
      ->linkExists('Translation');
    
    $this
      ->clickLink('Configuration');
    $this
      ->assertText('Regional and language');
    $this
      ->clickLink('Lingotek Translation');
    
    $assert_session
      ->linkExists('Content');
    $this
      ->clickLink('Content');
    $this
      ->assertText('Manage Translations');
  }
  
  public function testNavigationThroughSiteForBulkConfigTranslationAsTranslationsManager() {
    $assert_session = $this
      ->assertSession();
    
    $this
      ->drupalLogin($this->translationManagerUser);
    $this
      ->drupalGet('/user');
    
    $assert_session
      ->linkExists('Configuration');
    $assert_session
      ->linkExists('Translation');
    
    $this
      ->clickLink('Configuration');
    $this
      ->assertText('Regional and language');
    $this
      ->clickLink('Lingotek Translation');
    
    $this
      ->assertSession()
      ->linkNotExistsExact('Config');
  }
  
  public function testNavigationThroughSiteForBulkConfigTranslationAsTranslationsManagerWithTranslateConfigPermission() {
    $assert_session = $this
      ->assertSession();
    
    $roles = $this->translationManagerUser
      ->getRoles(TRUE);
    
    $role = Role::load($roles[0]);
    $role
      ->grantPermission('translate configuration')
      ->save();
    $this
      ->drupalLogin($this->translationManagerUser);
    $this
      ->drupalGet('/user');
    
    $assert_session
      ->linkExists('Configuration');
    $assert_session
      ->linkExists('Translation');
    
    $this
      ->clickLink('Configuration');
    $this
      ->assertText('Regional and language');
    $this
      ->clickLink('Lingotek Translation');
    
    $assert_session
      ->linkExists('Config');
    $this
      ->clickLink('Config');
    $this
      ->assertText('Manage Configuration Translation');
  }
  
  public function testDashboardAsTranslationsManager() {
    $assert_session = $this
      ->assertSession();
    
    $this
      ->drupalLogin($this->translationManagerUser);
    
    $request = $this
      ->drupalGet(Url::fromRoute('lingotek.dashboard_endpoint', [], [
      'absolute' => TRUE,
    ]));
    $response = json_decode($request, TRUE);
    $this
      ->verbose(var_export($response, TRUE));
    $this
      ->assertIdentical('GET', $response['method']);
    $this
      ->assertIdentical(2, $response['count']);
    $this
      ->assertIdentical('en', $response['languages']['en_US']['xcode']);
    $this
      ->assertIdentical(1, $response['languages']['en_US']['active']);
    $this
      ->assertIdentical(1, $response['languages']['en_US']['enabled']);
    $this
      ->assertIdentical('es', $response['languages']['es_ES']['xcode']);
    $this
      ->assertIdentical(1, $response['languages']['es_ES']['active']);
    $this
      ->assertIdentical(1, $response['languages']['es_ES']['enabled']);
  }
  
  public function testNodeTranslateDoesntContainBulkActions() {
    $assert_session = $this
      ->assertSession();
    
    $contentManager = $this
      ->createUser([
      'access toolbar',
      'access content overview',
      'administer nodes',
      'assign lingotek translation profiles',
      'create article content',
      'translate any entity',
    ]);
    $this
      ->drupalLogin($contentManager);
    $edit = [];
    $edit['title[0][value]'] = 'Llamas are cool';
    $edit['body[0][value]'] = 'Llamas are very cool';
    $edit['langcode[0][value]'] = 'en';
    $edit['lingotek_translation_management[lingotek_translation_profile]'] = 'manual';
    $this
      ->saveAndPublishNodeForm($edit);
    $this
      ->clickLink('Translate');
    
    $assert_session
      ->linkNotExists('Upload');
    $this
      ->assertNoFieldByName('op');
  }
  
  public function testConfigTranslateDoesntContainBulkActions() {
    $assert_session = $this
      ->assertSession();
    
    $contentManager = $this
      ->createUser([
      'access toolbar',
      'access administration pages',
      'administer site configuration',
      'translate configuration',
    ]);
    $this
      ->drupalLogin($contentManager);
    
    $this
      ->drupalGet('/admin/config/system/site-information');
    $this
      ->clickLink('Translate system information');
    
    $assert_session
      ->linkNotExists('Upload');
  }
}