View source
<?php
namespace Drupal\Tests\path\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
class PathContentModerationTest extends BrowserTestBase {
use ContentModerationTestTrait;
protected static $modules = [
'node',
'path',
'content_moderation',
'content_translation',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
ConfigurableLanguage::createFromLangcode('fr')
->save();
$this
->rebuildContainer();
$this
->drupalCreateContentType([
'name' => 'moderated',
'type' => 'moderated',
]);
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'moderated');
$workflow
->save();
$this
->drupalLogin($this->rootUser);
$edit = [
'language_interface[enabled][language-url]' => 1,
];
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm($edit, 'Save settings');
$edit = [
'entity_types[node]' => 1,
'settings[node][moderated][translatable]' => 1,
'settings[node][moderated][fields][path]' => 1,
'settings[node][moderated][fields][body]' => 1,
'settings[node][moderated][settings][language][language_alterable]' => 1,
];
$this
->drupalGet('admin/config/regional/content-language');
$this
->submitForm($edit, 'Save configuration');
\Drupal::entityTypeManager()
->clearCachedDefinitions();
}
public function testNodePathAlias() {
$this
->drupalGet('node/add/moderated');
$this
->assertSession()
->fieldValueEquals('path[0][alias]', '');
$this
->submitForm([
'title[0][value]' => 'moderated content',
'path[0][alias]' => '/moderated-content',
'moderation_state[0][state]' => 'published',
], 'Save');
$node = $this
->getNodeByTitle('moderated content');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldValueEquals('path[0][alias]', '/moderated-content');
$this
->submitForm([
'title[0][value]' => 'pending revision',
'path[0][alias]' => '/moderated-content',
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->assertSession()
->statusMessageNotContains('You can only change the URL alias for the published version of this content.');
$this
->drupalGet('node/add/moderated');
$this
->assertSession()
->fieldValueEquals('path[0][alias]', '');
$this
->submitForm([
'title[0][value]' => 'moderated content 2',
'path[0][alias]' => '',
'moderation_state[0][state]' => 'published',
], 'Save');
$node = $this
->getNodeByTitle('moderated content 2');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldValueEquals('path[0][alias]', '');
$this
->submitForm([
'title[0][value]' => 'pending revision',
'path[0][alias]' => '/pending-revision',
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->assertSession()
->statusMessageContains('You can only change the URL alias for the published version of this content.', 'error');
$this
->drupalGet('node/add/moderated');
$this
->assertSession()
->fieldValueEquals('path[0][alias]', '');
$this
->submitForm([
'title[0][value]' => 'moderated content 3',
'path[0][alias]' => '',
'moderation_state[0][state]' => 'published',
], 'Save');
$node = $this
->getNodeByTitle('moderated content 3');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldValueEquals('path[0][alias]', '');
$this
->submitForm([
'title[0][value]' => 'pending revision',
'path[0][alias]' => '',
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->assertSession()
->statusMessageNotContains('You can only change the URL alias for the published version of this content.');
}
public function testTranslatedModeratedNodeAlias() {
$default_node = $this
->drupalCreateNode([
'type' => 'moderated',
'langcode' => 'en',
'moderation_state' => 'published',
'path' => '/' . $this
->randomMachineName(),
]);
$this
->drupalGet('node/' . $default_node
->id());
$this
->drupalGet('node/' . $default_node
->id() . '/translations');
$this
->clickLink('Add');
$edit_translation = [
'body[0][value]' => $this
->randomMachineName(),
'moderation_state[0][state]' => 'published',
'path[0][alias]' => '/' . $this
->randomMachineName(),
];
$this
->submitForm($edit_translation, 'Save (this translation)');
$this
->drupalGet('fr' . $edit_translation['path[0][alias]']);
$this
->assertSession()
->pageTextContains($edit_translation['body[0][value]']);
$default_path = $default_node->path->alias;
$translation_path = 'fr' . $edit_translation['path[0][alias]'];
$this
->assertPathsAreAccessible([
$default_path,
$translation_path,
]);
$edit_new_translation_draft_with_alias = [
'moderation_state[0][state]' => 'draft',
'path[0][alias]' => '/' . $this
->randomMachineName(),
];
$this
->drupalGet('fr/node/' . $default_node
->id() . '/edit');
$this
->submitForm($edit_new_translation_draft_with_alias, 'Save (this translation)');
$this
->assertSession()
->statusMessageContains('You can only change the URL alias for the published version of this content.', 'error');
$edit_new_translation_draft = [
'body[0][value]' => $this
->randomMachineName(),
'moderation_state[0][state]' => 'draft',
];
$this
->drupalGet('fr/node/' . $default_node
->id() . '/edit');
$this
->submitForm($edit_new_translation_draft, 'Save (this translation)');
$this
->assertSession()
->statusMessageNotContains('You can only change the URL alias for the published version of this content.');
$this
->assertSession()
->pageTextContains($edit_new_translation_draft['body[0][value]']);
$this
->assertPathsAreAccessible([
$default_path,
$translation_path,
]);
$edit_new_translation_draft_with_defaults_alias = [
'moderation_state[0][state]' => 'draft',
'path[0][alias]' => $default_node->path->alias,
];
$this
->drupalGet('fr/node/' . $default_node
->id() . '/edit');
$this
->submitForm($edit_new_translation_draft_with_defaults_alias, 'Save (this translation)');
$this
->assertSession()
->statusMessageContains('You can only change the URL alias for the published version of this content.', 'error');
$edit_new_translation_draft_empty_alias = [
'body[0][value]' => $this
->randomMachineName(),
'moderation_state[0][state]' => 'draft',
'path[0][alias]' => '',
];
$this
->drupalGet('fr/node/' . $default_node
->id() . '/edit');
$this
->submitForm($edit_new_translation_draft_empty_alias, 'Save (this translation)');
$this
->assertSession()
->statusMessageContains('You can only change the URL alias for the published version of this content.', 'error');
$edit_new_translation = [
'body[0][value]' => $this
->randomMachineName(),
'moderation_state[0][state]' => 'published',
'path[0][alias]' => '/' . $this
->randomMachineName(),
];
$this
->drupalGet('fr/node/' . $default_node
->id() . '/edit');
$this
->submitForm($edit_new_translation, 'Save (this translation)');
$this
->assertSession()
->statusMessageNotContains('You can only change the URL alias for the published version of this content.');
$this
->assertSession()
->pageTextContains($edit_new_translation['body[0][value]']);
$this
->assertSession()
->addressEquals('fr' . $edit_new_translation['path[0][alias]']);
$this
->assertPathsAreAccessible([
$default_path,
]);
}
public function assertPathsAreAccessible(array $paths) : void {
foreach ($paths as $path) {
$this
->drupalGet($path);
$this
->assertSession()
->statusCodeEquals(200);
}
}
}