Issue #2940830 by Sam152, catch, amateescu, timmillwood: Provide explicit test coverage for isDefaultRevisionPublished and its usage within ModerationStateFieldItemList

merge-requests/1654/head
Alex Pott 2018-05-08 00:04:56 +01:00
parent 7aa16e856b
commit bdf6c01cb1
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
2 changed files with 150 additions and 23 deletions

View File

@ -2,8 +2,10 @@
namespace Drupal\Tests\content_moderation\Kernel; namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\entity_test\Entity\EntityTestMulRevPub;
use Drupal\entity_test\Entity\EntityTestRev; use Drupal\entity_test\Entity\EntityTestRev;
use Drupal\KernelTests\KernelTestBase; use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\workflows\Entity\Workflow; use Drupal\workflows\Entity\Workflow;
/** /**
@ -15,7 +17,21 @@ class ModerationInformationTest extends KernelTestBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static $modules = ['content_moderation', 'entity_test', 'user', 'workflows']; public static $modules = [
'content_moderation',
'entity_test',
'user',
'workflows',
'language',
'content_translation',
];
/**
* The moderation information service.
*
* @var \Drupal\content_moderation\ModerationInformationInterface
*/
protected $moderationInformation;
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -24,8 +40,20 @@ class ModerationInformationTest extends KernelTestBase {
parent::setUp(); parent::setUp();
$this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('entity_test_rev');
$this->installEntitySchema('entity_test_mulrevpub');
$this->installEntitySchema('content_moderation_state'); $this->installEntitySchema('content_moderation_state');
$this->installConfig(['content_moderation']); $this->installConfig(['content_moderation']);
$this->moderationInformation = $this->container->get('content_moderation.moderation_information');
ConfigurableLanguage::createFromLangcode('de')->save();
$workflow = Workflow::load('editorial');
$workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_mulrevpub', 'entity_test_mulrevpub');
$workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
$workflow->save();
$this->container->get('content_translation.manager')->setEnabled('entity_test_mulrevpub', 'entity_test_mulrevpub', TRUE);
} }
/** /**
@ -33,10 +61,6 @@ class ModerationInformationTest extends KernelTestBase {
* @covers ::getLatestRevisionId * @covers ::getLatestRevisionId
*/ */
public function testDefaultAndLatestRevisionId() { public function testDefaultAndLatestRevisionId() {
$workflow = Workflow::load('editorial');
$workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
$workflow->save();
$entity_test_rev = EntityTestRev::create([ $entity_test_rev = EntityTestRev::create([
'name' => 'Default Revision', 'name' => 'Default Revision',
'moderation_state' => 'published', 'moderation_state' => 'published',
@ -47,18 +71,85 @@ class ModerationInformationTest extends KernelTestBase {
$entity_test_rev->moderation_state = 'draft'; $entity_test_rev->moderation_state = 'draft';
$entity_test_rev->save(); $entity_test_rev->save();
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
$moderation_info = \Drupal::service('content_moderation.moderation_information');
// Check that moderation information service returns the correct default // Check that moderation information service returns the correct default
// revision ID. // revision ID.
$default_revision_id = $moderation_info->getDefaultRevisionId('entity_test_rev', $entity_test_rev->id()); $default_revision_id = $this->moderationInformation->getDefaultRevisionId('entity_test_rev', $entity_test_rev->id());
$this->assertSame(1, $default_revision_id); $this->assertSame(1, $default_revision_id);
// Check that moderation information service returns the correct latest // Check that moderation information service returns the correct latest
// revision ID. // revision ID.
$latest_revision_id = $moderation_info->getLatestRevisionId('entity_test_rev', $entity_test_rev->id()); $latest_revision_id = $this->moderationInformation->getLatestRevisionId('entity_test_rev', $entity_test_rev->id());
$this->assertSame(2, $latest_revision_id); $this->assertSame(2, $latest_revision_id);
} }
/**
* @covers ::isDefaultRevisionPublished
* @dataProvider isDefaultRevisionPublishedTestCases
*/
public function testIsDefaultRevisionPublished($initial_state, $final_state, $initial_is_default_published, $final_is_default_published) {
$entity = EntityTestMulRevPub::create([
'moderation_state' => $initial_state,
]);
$entity->save();
$this->assertEquals($initial_is_default_published, $this->moderationInformation->isDefaultRevisionPublished($entity));
$entity->moderation_state = $final_state;
$entity->save();
$this->assertEquals($final_is_default_published, $this->moderationInformation->isDefaultRevisionPublished($entity));
}
/**
* Test cases for ::testIsDefaultRevisionPublished.
*/
public function isDefaultRevisionPublishedTestCases() {
return [
'Draft to draft' => [
'draft',
'draft',
FALSE,
FALSE,
],
'Draft to published' => [
'draft',
'published',
FALSE,
TRUE,
],
'Published to published' => [
'published',
'published',
TRUE,
TRUE,
],
'Published to draft' => [
'published',
'draft',
TRUE,
TRUE,
],
];
}
/**
* @covers ::isDefaultRevisionPublished
*/
public function testIsDefaultRevisionPublishedMultilingual() {
$entity = EntityTestMulRevPub::create([
'moderation_state' => 'draft',
]);
$entity->save();
$this->assertEquals('draft', $entity->moderation_state->value);
$translated = $entity->addTranslation('de');
$translated->moderation_state = 'published';
$translated->save();
$this->assertEquals('published', $translated->moderation_state->value);
// Test a scenario where the default revision exists with the default
// language in a draft state and a non-default language in a published
// state. The method returns TRUE if any of the languages for the default
// revision are in a published state.
$this->assertEquals(TRUE, $this->moderationInformation->isDefaultRevisionPublished($entity));
}
} }

View File

@ -130,22 +130,58 @@ class ModerationStateFieldItemListTest extends KernelTestBase {
/** /**
* Tests that moderation state changes also change the related entity state. * Tests that moderation state changes also change the related entity state.
*
* @dataProvider moderationStateChangesTestCases
*/ */
public function testModerationStateChanges() { public function testModerationStateChanges($initial_state, $final_state, $first_published, $first_is_default, $second_published, $second_is_default) {
// Change the moderation state and check that the entity's $this->testNode->moderation_state->value = $initial_state;
// 'isDefaultRevision' flag and the publishing status have also been $this->assertEquals($first_published, $this->testNode->isPublished());
// updated. $this->assertEquals($first_is_default, $this->testNode->isDefaultRevision());
$this->testNode->moderation_state->value = 'published';
$this->assertTrue($this->testNode->isPublished());
$this->assertTrue($this->testNode->isDefaultRevision());
$this->testNode->save(); $this->testNode->save();
// Repeat the checks using an 'unpublished' state. $this->testNode->moderation_state->value = $final_state;
$this->testNode->moderation_state->value = 'draft'; $this->assertEquals($second_published, $this->testNode->isPublished());
$this->assertFalse($this->testNode->isPublished()); $this->assertEquals($second_is_default, $this->testNode->isDefaultRevision());
$this->assertFalse($this->testNode->isDefaultRevision()); }
/**
* Data provider for ::testModerationStateChanges
*/
public function moderationStateChangesTestCases() {
return [
'Draft to draft' => [
'draft',
'draft',
FALSE,
TRUE,
FALSE,
TRUE,
],
'Draft to published' => [
'draft',
'published',
FALSE,
TRUE,
TRUE,
TRUE,
],
'Published to published' => [
'published',
'published',
TRUE,
TRUE,
TRUE,
TRUE,
],
'Published to draft' => [
'published',
'draft',
TRUE,
TRUE,
FALSE,
FALSE,
],
];
} }
/** /**