View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\scheduled_transitions\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\scheduled_transitions\Entity\ScheduledTransition;
use Drupal\scheduled_transitions_test\Entity\ScheduledTransitionsTestEntity as TestEntity;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\Tests\scheduled_transitions\Traits\ScheduledTransitionTestTrait;
use Drupal\user\Entity\User;
class ScheduledTransitionLocalTaskTest extends BrowserTestBase {
use ContentModerationTestTrait;
use ScheduledTransitionTestTrait;
protected static $modules = [
'scheduled_transitions_test',
'scheduled_transitions',
'content_moderation',
'workflows',
'dynamic_entity_reference',
'user',
'block',
'language',
'field',
'system',
'locale',
'content_translation',
];
protected $defaultTheme = 'classy';
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
}
public function testTabTitleByTranslation() {
ConfigurableLanguage::createFromLangcode('de')
->save();
ConfigurableLanguage::createFromLangcode('fr')
->save();
$languageNegotiator = \Drupal::service('language_negotiator');
$languageNegotiator
->saveConfiguration('language_content', [
LanguageNegotiationUrl::METHOD_ID => 1,
]);
$this
->rebuildContainer();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('st_entity_test', 'st_entity_test');
$workflow
->save();
$this
->enabledBundles([
[
'st_entity_test',
'st_entity_test',
],
]);
$entity = TestEntity::create([
'type' => 'st_entity_test',
]);
$de = $entity
->addTranslation('de');
$fr = $entity
->addTranslation('fr');
$de->name = 'deName';
$fr->name = 'frName';
$entity
->save();
$author = User::create([
'uid' => 2,
'name' => $this
->randomMachineName(),
]);
$author
->save();
$scheduledTransition = ScheduledTransition::create([
'entity' => $entity,
'entity_revision_id' => 1,
'entity_revision_langcode' => 'de',
'author' => $author,
'workflow' => $workflow
->id(),
'moderation_state' => 'published',
'transition_on' => (new \DateTime('2 Feb 2018 11am'))
->getTimestamp(),
]);
$scheduledTransition
->save();
$currentUser = $this
->drupalCreateUser([
'administer st_entity_test entities',
'use editorial transition create_new_draft',
'view scheduled transitions st_entity_test st_entity_test',
]);
$this
->drupalLogin($currentUser);
$this
->drupalGet($entity
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementTextContains('css', 'nav.tabs', 'Scheduled transitions (0)');
$this
->drupalGet($entity
->getTranslation('de')
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementTextContains('css', 'nav.tabs', 'Scheduled transitions (1)');
$this
->drupalGet($entity
->getTranslation('fr')
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementTextContains('css', 'nav.tabs', 'Scheduled transitions (0)');
}
public function testLatestRevisionTab() {
$entityStorage = \Drupal::entityTypeManager()
->getStorage('st_entity_test');
$this
->enabledBundles([
[
'st_entity_test',
'st_entity_test',
],
]);
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('st_entity_test', 'st_entity_test');
$workflow
->save();
$currentUser = $this
->drupalCreateUser([
'administer st_entity_test entities',
'use editorial transition create_new_draft',
'use editorial transition publish',
'use editorial transition archive',
'view latest version',
'view any unpublished content',
]);
$this
->drupalLogin($currentUser);
$entity = TestEntity::create([
'type' => 'st_entity_test',
]);
$entity = $entityStorage
->createRevision($entity, FALSE);
$entity->name = 'rev1';
$entity->moderation_state = 'draft';
$entity
->save();
$this
->drupalGet($entity
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->linkExists('View');
$this
->assertSession()
->linkExists('Edit');
$this
->assertSession()
->linkNotExists('Latest version');
$entity = $entityStorage
->createRevision($entity, FALSE);
$entity->name = 'rev2';
$entity->moderation_state = 'published';
$entity
->save();
$this
->drupalGet($entity
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->linkExists('View');
$this
->assertSession()
->linkExists('Edit');
$this
->assertSession()
->linkNotExists('Latest version');
$entity = $entityStorage
->createRevision($entity, FALSE);
$entity->moderation_state = 'draft';
$entity
->save();
$this
->drupalGet($entity
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->linkExists('View');
$this
->assertSession()
->linkExists('Edit');
$this
->assertSession()
->linkExists('Latest version');
$scheduledTransition = ScheduledTransition::create([
'entity' => $entity,
'entity_revision_id' => $entity
->getRevisionId(),
'author' => 1,
'workflow' => $workflow
->id(),
'moderation_state' => 'published',
'transition_on' => (new \DateTime('1 year ago'))
->getTimestamp(),
]);
$scheduledTransition
->save();
$this
->runTransition($scheduledTransition);
$this
->drupalGet($entity
->toUrl());
$this
->assertSession()
->linkExists('View');
$this
->assertSession()
->linkExists('Edit');
$this
->assertSession()
->linkNotExists('Latest version');
}
}