View source
<?php
namespace Drupal\Tests\view_unpublished\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\Tests\BrowserTestBase;
class ViewUnpublishedMultilingualTest extends BrowserTestBase {
use StringTranslationTrait;
public static $modules = [
'view_unpublished',
'node',
'content_translation',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer languages',
'administer content types',
'administer nodes',
'create page content',
'edit any page content',
'translate any entity',
'create content translations',
]);
$this
->drupalLogin($this->adminUser);
ConfigurableLanguage::createFromLangcode('it')
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'page', TRUE);
\Drupal::service('router.builder')
->rebuild();
}
public function testIt() {
$langcode = language_get_default_langcode('node', 'page');
$title_key = 'title[0][value]';
$body_key = 'body[0][value]';
$status_key = 'status[value]';
$edit = [];
$edit[$title_key] = $this
->randomMachineName(8);
$edit[$body_key] = $this
->randomMachineName(16);
$edit[$status_key] = 1;
$this
->drupalPostForm('node/add/page', $edit, $this
->t('Save'));
$node = $this
->getNodeByTitle($edit[$title_key]);
$this
->assertTrue($node
->language()
->getId() == $langcode, 'Language correctly set.');
$this
->drupalLogout();
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->adminUser);
$edit[$body_key] = $this
->randomMachineName(16);
$edit[$status_key] = 0;
$this
->drupalPostForm($node
->toUrl('edit-form'), $edit, $this
->t('Save'));
$this
->drupalLogout();
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this
->drupalCreateUser([
'view any unpublished content',
]));
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->adminUser);
$edit[$body_key] = $this
->randomMachineName(16);
$edit[$status_key] = 1;
$this
->drupalPostForm($node
->toUrl('edit-form'), $edit, $this
->t('Save'));
$this
->drupalLogout();
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->adminUser);
$add_translation_url = Url::fromRoute('entity.node.content_translation_add', [
'node' => $node
->id(),
'source' => 'en',
'target' => 'it',
], [
'language' => ConfigurableLanguage::load('it'),
'absolute' => FALSE,
]);
$edit[$body_key] = $this
->randomMachineName(16);
$edit[$status_key] = 0;
$edit[$status_key] = 0;
$this
->drupalPostForm($add_translation_url, $edit, $this
->t('Save (this translation)'));
$node = $this
->getNodeByTitle($edit[$title_key], TRUE);
$this
->drupalLogout();
$this->container
->get('language_negotiator')
->saveConfiguration(LanguageInterface::TYPE_CONTENT, [
LanguageNegotiationUrl::METHOD_ID => 0,
]);
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$translation_url = "it/node/{$node->id()}";
$this
->drupalGet($translation_url);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this
->drupalCreateUser([
'view any unpublished content',
]));
$this
->drupalGet($translation_url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this
->drupalCreateUser([
'view any unpublished page content',
]));
$this
->drupalGet($translation_url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this
->drupalCreateUser());
$this
->drupalGet($translation_url);
$this
->assertSession()
->statusCodeEquals(403);
node_access_rebuild();
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet($translation_url);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this
->drupalCreateUser([
'view any unpublished content',
]));
$this
->drupalGet($translation_url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this
->drupalCreateUser([
'view any unpublished page content',
]));
$this
->drupalGet($translation_url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this
->drupalCreateUser());
$this
->drupalGet($translation_url);
$this
->assertSession()
->statusCodeEquals(403);
}
}