Commit 8cb1b179 authored by Crell's avatar Crell

Fix up tests for new form structure.

parent 0687e197
......@@ -54,8 +54,8 @@ class ModerationStateBlockTest extends ModerationStateTestBase {
// Enable moderation for custom blocks at admin/structure/block/block-content/manage/basic/moderation.
$edit = [
'enable_moderation_state' => TRUE,
'allowed_moderation_states[draft]' => TRUE,
'allowed_moderation_states[published]' => TRUE,
'allowed_moderation_states_unpublished[draft]' => TRUE,
'allowed_moderation_states_published[published]' => TRUE,
'default_moderation_state' => 'draft',
];
$this->drupalPostForm('admin/structure/block/block-content/manage/basic/moderation', $edit, t('Save'));
......
......@@ -35,23 +35,23 @@ class ModerationStateNodeTypeTest extends ModerationStateTestBase {
* A node type without moderation state enabled.
*/
public function testEnablingOnExistingContent() {
// Create a node type that is not moderated.
$this->drupalLogin($this->adminUser);
$this->createContentTypeFromUI('Not moderated', 'not_moderated');
$this->grantUserPermissionToCreateContentOfType($this->adminUser, 'not_moderated');
// Create content.
$this->drupalGet('node/add/not_moderated');
$this->drupalPostForm(NULL, [
'title[0][value]' => 'Test',
], t('Save and publish'));
$this->assertText('Not moderated Test has been created.');
// Now enable moderation state.
$this->drupalGet('admin/structure/types/manage/not_moderated/moderation');
$this->drupalPostForm(NULL, [
'enable_moderation_state' => 1,
'allowed_moderation_states[draft]' => 1,
'allowed_moderation_states[needs_review]' => 1,
'allowed_moderation_states[published]' => 1,
'default_moderation_state' => 'draft',
], t('Save'));
$this->enableModerationThroughUI('not_moderated', ['draft', 'needs_review', 'published'], 'draft');
// And make sure it works.
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
'title' => 'Test'
]);
......@@ -68,60 +68,4 @@ class ModerationStateNodeTypeTest extends ModerationStateTestBase {
$this->assertRaw('Save and Create New Draft');
$this->assertNoRaw('Save and publish');
}
/**
* Creates a content-type from the UI.
*
* @param string $content_type_name
* Content type human name.
* @param string $content_type_id
* Machine name.
* @param bool $moderated
* TRUE if should be moderated
* @param string[] $allowed_states
* Array of allowed state IDs
* @param string $default_state
* Default state.
*/
protected function createContentTypeFromUI($content_type_name, $content_type_id, $moderated = FALSE, $allowed_states = [], $default_state = NULL) {
$this->drupalGet('admin/structure/types');
$this->clickLink('Add content type');
$edit = [
'name' => $content_type_name,
'type' => $content_type_id,
];
$this->drupalPostForm(NULL, $edit, t('Save content type'));
if ($moderated) {
$this->drupalGet('admin/structure/types/' . $content_type_id . '/moderation');
$this->assertFieldByName('enable_moderation_state');
$this->assertNoFieldChecked('edit-enable-moderation-state');
$edit['enable_moderation_state'] = 1;
foreach ($allowed_states as $state) {
$edit['allowed_moderation_states[' . $state . ']'] = 1;
}
$edit['default_moderation_state'] = $default_state;
$this->drupalPostForm('admin/structure/types/' . $content_type_id . '/moderation', $edit, t('Save'));
}
}
/**
* Grants given user permission to create content of given type.
*
* @param \Drupal\Core\Session\AccountInterface $account
* User to grant permission to.
* @param string $content_type_id
* Content type ID.
*/
protected function grantUserPermissionToCreateContentOfType(AccountInterface $account, $content_type_id) {
$role_ids = $account->getRoles(TRUE);
/* @var \Drupal\user\RoleInterface $role */
$role_id = reset($role_ids);
$role = Role::load($role_id);
$role->grantPermission(sprintf('create %s content', $content_type_id));
$role->grantPermission(sprintf('edit any %s content', $content_type_id));
$role->save();
}
}
......@@ -84,7 +84,7 @@ abstract class ModerationStateTestBase extends WebTestBase {
* @param string $default_state
* Default state.
*/
protected function createContentTypeFromUI($content_type_name, $content_type_id, $moderated = FALSE, $allowed_states = [], $default_state = NULL) {
protected function createContentTypeFromUI($content_type_name, $content_type_id, $moderated = FALSE, array $allowed_states = [], $default_state = NULL) {
$this->drupalGet('admin/structure/types');
$this->clickLink('Add content type');
$edit = [
......@@ -93,25 +93,40 @@ abstract class ModerationStateTestBase extends WebTestBase {
];
$this->drupalPostForm(NULL, $edit, t('Save content type'));
if ($moderated) {
$this->enableModerationThroughUI($content_type_id, $allowed_states, $default_state);
}
}
/**
* Enable moderation for a specified content type, using the UI.
*
* @param string $content_type_id
* Machine name.
* @param string[] $allowed_states
* Array of allowed state IDs.
* @param string $default_state
* Default state.
*/
protected function enableModerationThroughUI($content_type_id, array $allowed_states, $default_state) {
$this->drupalGet('admin/structure/types/manage/' . $content_type_id . '/moderation');
$this->assertFieldByName('enable_moderation_state');
$this->assertNoFieldChecked('edit-enable-moderation-state');
$edit = [];
if ($moderated) {
$edit['enable_moderation_state'] = 1;
/** @var ModerationState $state */
foreach (ModerationState::loadMultiple() as $id => $state) {
$value = (int)in_array($id, $allowed_states);
$key = $state->isPublishedState() ? 'allowed_moderation_states_published[' . $state->id() . ']' : 'allowed_moderation_states_unpublished[' . $state->id() . ']';
$edit[$key] = $value;
}
$edit['enable_moderation_state'] = 1;
$edit['default_moderation_state'] = $default_state;
/** @var ModerationState $state */
foreach (ModerationState::loadMultiple() as $id => $state) {
$key = $state->isPublishedState() ? 'allowed_moderation_states_published[' . $state->id() . ']' : 'allowed_moderation_states_unpublished[' . $state->id() . ']';
$edit[$key] = (int)in_array($id, $allowed_states);
}
$edit['default_moderation_state'] = $default_state;
$this->drupalPostForm(NULL, $edit, t('Save'));
}
/**
* Grants given user permission to create content of given type.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment