View source
<?php
namespace Drupal\Tests\scheduler\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
class SchedulerMultilingualTest extends SchedulerBrowserTestBase {
protected static $modules = [
'content_translation',
];
protected $translatorUser;
protected $languages;
protected function setUp() : void {
parent::setUp();
$this->translatorUser = $this
->drupalCreateUser([
'administer languages',
'administer content translation',
'create content translations',
'translate any entity',
]);
$admin_roles = $this->adminUser
->getRoles();
$this->translatorUser
->addRole($admin_roles[1]);
$this->translatorUser
->save();
$this
->drupalLogin($this->translatorUser);
$this->nodetype
->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
->save();
$this->container
->get('content_translation.manager')
->setEnabled('node', $this->type, TRUE);
$langcodes = [
'am',
'bg',
'ca',
'en',
];
ConfigurableLanguage::createFromLangcode($langcodes[0])
->save();
ConfigurableLanguage::createFromLangcode($langcodes[1])
->save();
ConfigurableLanguage::createFromLangcode($langcodes[2])
->save();
$languages = \Drupal::languageManager()
->getLanguages();
foreach ($langcodes as $key => $code) {
$this->languages[$key] = [
'code' => $code,
'name' => $languages[$code]
->getName(),
];
}
}
private function checkStatus($nid, $description, array $status) {
$this->nodeStorage
->resetCache([
$nid,
]);
$node = $this->nodeStorage
->load($nid);
foreach ($status as $key => $expected_status) {
if ($key == 0) {
$this
->assertEquals($expected_status, $node
->isPublished(), sprintf('%s: The original content (%s) is %s', $description, $this->languages[$key]['name'], $expected_status ? 'published' : 'unpublished'));
}
else {
$translation = $node
->getTranslation($this->languages[$key]['code']);
$this
->assertEquals($expected_status, $translation
->isPublished(), sprintf('%s: Translation %d (%s) is %s', $description, $key, $this->languages[$key]['name'], $expected_status ? 'published' : 'unpublished'));
}
}
}
public function testPublishingTranslations($publish_on_translatable, $unpublish_on_translatable, $status_translatable, array $expected_status_values_before, array $expected_status_values_after) {
$this
->drupalGet('admin/config/regional/language');
$this
->drupalGet('admin/config/regional/content-language');
$settings = [
'edit-settings-node-' . $this->type . '-settings-language-language-alterable' => TRUE,
'edit-settings-node-' . $this->type . '-fields-publish-on' => $publish_on_translatable,
'edit-settings-node-' . $this->type . '-fields-unpublish-on' => $unpublish_on_translatable,
'edit-settings-node-' . $this->type . '-fields-status' => $status_translatable,
];
$this
->submitForm($settings, 'Save configuration');
$early_return = FALSE;
if ($publish_on_translatable != $status_translatable) {
$this
->assertSession()
->elementExists('xpath', '//input[@id = "edit-settings-node-' . $this->type . '-fields-publish-on" and contains(@class, "error")]');
$this
->assertSession()
->elementExists('xpath', '//input[@id = "edit-settings-node-' . $this->type . '-fields-status" and contains(@class, "error")]');
$early_return = TRUE;
}
if ($unpublish_on_translatable != $status_translatable) {
$this
->assertSession()
->elementExists('xpath', '//input[@id = "edit-settings-node-' . $this->type . '-fields-unpublish-on" and contains(@class, "error")]');
$this
->assertSession()
->elementExists('xpath', '//input[@id = "edit-settings-node-' . $this->type . '-fields-status" and contains(@class, "error")]');
$early_return = TRUE;
}
if ($early_return) {
return;
}
$create = [
'type' => $this->type,
'title' => $this->languages[0]['name'] . '(0) - Unpublished and not scheduled',
'langcode' => $this->languages[0]['code'],
'status' => FALSE,
];
$node = $this
->drupalCreateNode($create);
$nid = $node
->id();
$this
->drupalGet('node/' . $nid . '/translations/add/' . $this->languages[0]['code'] . '/' . $this->languages[1]['code']);
$edit = [
'title[0][value]' => $this->languages[1]['name'] . '(1) - Published now',
'publish_on[0][value][date]' => '',
'publish_on[0][value][time]' => '',
'status[value]' => TRUE,
];
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/' . $nid . '/translations/add/' . $this->languages[0]['code'] . '/' . $this->languages[2]['code']);
$edit = [
'title[0][value]' => $this->languages[2]['name'] . '(2) - Publish in the future',
'publish_on[0][value][date]' => date('Y-m-d', strtotime('+2 day', $this->requestTime)),
'publish_on[0][value][time]' => date('H:i:s', strtotime('+2 day', $this->requestTime)),
'unpublish_on[0][value][date]' => date('Y-m-d', strtotime('+3 day', $this->requestTime)),
'unpublish_on[0][value][time]' => date('H:i:s', strtotime('+3 day', $this->requestTime)),
];
$this
->submitForm($edit, 'Save');
$this->nodeStorage
->resetCache([
$nid,
]);
$node = $this->nodeStorage
->load($nid);
$translation1 = $node
->getTranslation($this->languages[1]['code']);
$translation2 = $node
->getTranslation($this->languages[2]['code']);
if ($publish_on_translatable) {
$this
->assertNotEquals($translation2->publish_on->value, $node->publish_on->value, 'Node publish_on');
$this
->assertNotEquals($translation2->unpublish_on->value, $node->unpublish_on->value, 'Node unpublish_on');
}
else {
$this
->assertEquals($translation2->publish_on->value, $node->publish_on->value, 'Node publish_on');
$this
->assertEquals($translation2->unpublish_on->value, $node->unpublish_on->value, 'Node unpublish_on');
$this
->assertEquals($translation2->publish_on->value, $translation1->publish_on->value, 'Translation 1 publish_on');
$this
->assertEquals($translation2->unpublish_on->value, $translation1->unpublish_on->value, 'Translation 1 unpublish_on');
}
$this
->drupalGet('node/' . $nid . '/translations/add/' . $this->languages[0]['code'] . '/' . $this->languages[3]['code']);
$edit = [
'title[0][value]' => $this->languages[3]['name'] . '(3) - Publish in the past',
'publish_on[0][value][date]' => date('Y-m-d', strtotime('-2 day', $this->requestTime)),
'publish_on[0][value][time]' => date('H:i:s', strtotime('-2 day', $this->requestTime)),
];
$this
->submitForm($edit, 'Save');
$this->nodeStorage
->resetCache([
$nid,
]);
$node = $this->nodeStorage
->load($nid);
$translation1 = $node
->getTranslation($this->languages[1]['code']);
$translation2 = $node
->getTranslation($this->languages[2]['code']);
$translation3 = $node
->getTranslation($this->languages[3]['code']);
if ($publish_on_translatable) {
$this
->assertNotEquals($translation3->publish_on->value, $translation2->publish_on->value, 'Node publish_on');
$this
->assertNotEquals($translation3->unpublish_on->value, $translation2->unpublish_on->value, 'Node unpublish_on');
}
else {
$this
->assertEquals($translation3->publish_on->value, $node->publish_on->value, 'Node publish_on');
$this
->assertEquals($translation3->unpublish_on->value, $node->unpublish_on->value, 'Node unpublish_on');
$this
->assertEquals($translation3->publish_on->value, $translation1->publish_on->value, 'Translation 1 publish_on');
$this
->assertEquals($translation3->unpublish_on->value, $translation1->unpublish_on->value, 'Translation 1 unpublish_on');
$this
->assertEquals($translation3->publish_on->value, $translation2->publish_on->value, 'Translation 2 publish_on');
$this
->assertEquals($translation3->unpublish_on->value, $translation2->unpublish_on->value, 'Translation 2 unpublish_on');
}
$this
->drupalGet($this->languages[0]['code'] . '/node/' . $nid . '/translations');
$this
->drupalGet('admin/content/scheduled');
$this
->checkStatus($nid, 'Before cron', $expected_status_values_before);
$this
->cronRun();
$this
->checkStatus($nid, 'After cron', $expected_status_values_after);
$this
->drupalGet('admin/content/scheduled');
$this
->drupalGet('admin/content');
$this
->drupalGet('admin/reports/dblog');
$this
->drupalGet($this->languages[0]['code'] . '/node/' . $nid . '/translations');
}
public function dataPublishingTranslations() {
return [
'all fields' => [
TRUE,
TRUE,
TRUE,
[
FALSE,
TRUE,
FALSE,
FALSE,
],
[
FALSE,
TRUE,
FALSE,
TRUE,
],
],
'no fields' => [
FALSE,
FALSE,
FALSE,
[
FALSE,
FALSE,
FALSE,
FALSE,
],
[
TRUE,
TRUE,
TRUE,
TRUE,
],
],
'only publish_on' => [
TRUE,
FALSE,
FALSE,
[],
[],
],
'only unpublish_on' => [
FALSE,
TRUE,
FALSE,
[],
[],
],
'only status' => [
FALSE,
FALSE,
TRUE,
[],
[],
],
'publish_on and unpublish_on' => [
TRUE,
TRUE,
FALSE,
[],
[],
],
'publish_on and status' => [
TRUE,
FALSE,
TRUE,
[],
[],
],
'unpublish_on and status' => [
FALSE,
TRUE,
TRUE,
[],
[],
],
];
}
}