View source
<?php
namespace Drupal\Tests\scheduler\Functional;
class SchedulerFieldsDisplayTest extends SchedulerBrowserTestBase {
protected static $modules = [
'field_ui',
];
protected function setUp() : void {
parent::setUp();
$this->adminUser2 = $this
->drupalCreateUser([
'access content',
'administer content types',
'administer node form display',
'create ' . $this->type . ' content',
'schedule publishing of nodes',
]);
}
public function testVerticalTabOrFieldset() {
$this
->drupalLogin($this->adminUser);
$assert = $this
->assertSession();
$this
->drupalGet('node/add/' . $this->type);
$assert
->elementExists('xpath', '//div[contains(@class, "form-type-vertical-tabs")]//details[@id = "edit-scheduler-settings"]');
$this->nodetype
->setThirdPartySetting('scheduler', 'fields_display_mode', 'fieldset')
->save();
$this
->drupalGet('node/add/' . $this->type);
$assert
->elementNotExists('xpath', '//div[contains(@class, "form-type-vertical-tabs")]//details[@id = "edit-scheduler-settings"]');
$assert
->elementExists('xpath', '//details[@id = "edit-scheduler-settings" and not(@open = "open")]');
$this->nodetype
->setThirdPartySetting('scheduler', 'publish_required', TRUE)
->save();
$this
->drupalGet('node/add/' . $this->type);
$assert
->elementExists('xpath', '//details[@id = "edit-scheduler-settings" and @open = "open"]');
$this->nodetype
->setThirdPartySetting('scheduler', 'publish_required', FALSE)
->setThirdPartySetting('scheduler', 'unpublish_required', TRUE)
->save();
$this
->drupalGet('node/add/' . $this->type);
$assert
->elementExists('xpath', '//details[@id = "edit-scheduler-settings" and @open = "open"]');
$this->nodetype
->setThirdPartySetting('scheduler', 'publish_required', FALSE)
->setThirdPartySetting('scheduler', 'unpublish_required', FALSE)
->setThirdPartySetting('scheduler', 'expand_fieldset', 'always')
->save();
$this
->drupalGet('node/add/' . $this->type);
$assert
->elementExists('xpath', '//details[@id = "edit-scheduler-settings" and @open = "open"]');
$this->nodetype
->setThirdPartySetting('scheduler', 'expand_fieldset', 'when_required')
->save();
$options = [
'title' => 'Contains Publish-on date ' . $this
->randomMachineName(10),
'type' => $this->type,
'publish_on' => strtotime('+1 day'),
];
$node = $this
->drupalCreateNode($options);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$assert
->elementExists('xpath', '//details[@id = "edit-scheduler-settings" and @open = "open"]');
$options = [
'title' => 'Contains Publish-on date with timestamp value zero - ' . $this
->randomMachineName(10),
'type' => $this->type,
'publish_on' => 0,
];
$node = $this
->drupalCreateNode($options);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$assert
->elementExists('xpath', '//details[@id = "edit-scheduler-settings" and @open = "open"]');
$options = [
'title' => 'Contains Unpublish-on date ' . $this
->randomMachineName(10),
'type' => $this->type,
'unpublish_on' => strtotime('+1 day'),
];
$node = $this
->drupalCreateNode($options);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$assert
->elementExists('xpath', '//details[@id = "edit-scheduler-settings" and @open = "open"]');
$options = [
'title' => 'Contains Unpublish-on date with timestamp value zero - ' . $this
->randomMachineName(10),
'type' => $this->type,
'unpublish_on' => 0,
];
$node = $this
->drupalCreateNode($options);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$assert
->elementExists('xpath', '//details[@id = "edit-scheduler-settings" and @open = "open"]');
$this->nodetype
->setThirdPartySetting('scheduler', 'fields_display_mode', 'vertical_tab')
->save();
$this
->drupalGet('node/add/' . $this->type);
$assert
->elementExists('xpath', '//div[contains(@class, "form-type-vertical-tabs")]//details[@id = "edit-scheduler-settings"]');
}
public function testManageFormDisplay() {
$this
->drupalLogin($this->adminUser2);
$this
->drupalGet('admin/structure/types/manage/' . $this->type . '/form-display');
$this
->assertSession()
->fieldExists('edit-fields-scheduler-settings-weight');
$this->nodetype
->setThirdPartySetting('scheduler', 'publish_enable', FALSE)
->setThirdPartySetting('scheduler', 'unpublish_enable', FALSE)
->save();
$this
->drupalGet('admin/structure/types/manage/' . $this->type . '/form-display');
$this
->assertSession()
->FieldNotExists('edit-fields-scheduler-settings-weight');
}
public function testDisabledFields() {
$this
->drupalLogin($this->adminUser2);
$assert = $this
->assertSession();
$edit = [
'fields[publish_on][region]' => 'hidden',
];
$this
->drupalPostForm('admin/structure/types/manage/' . $this->type . '/form-display', $edit, 'Save');
$this
->drupalGet('node/add/' . $this->type);
$assert
->elementExists('xpath', '//div[contains(@class, "form-type-vertical-tabs")]//details[@id = "edit-scheduler-settings"]');
$this
->assertSession()
->FieldNotExists('publish_on[0][value][date]');
$this
->assertSession()
->FieldExists('unpublish_on[0][value][date]');
$edit = [
'fields[publish_on][region]' => 'content',
'fields[unpublish_on][region]' => 'hidden',
];
$this
->drupalPostForm('admin/structure/types/manage/' . $this->type . '/form-display', $edit, 'Save');
$this
->drupalGet('node/add/' . $this->type);
$assert
->elementExists('xpath', '//div[contains(@class, "form-type-vertical-tabs")]//details[@id = "edit-scheduler-settings"]');
$this
->assertSession()
->FieldExists('publish_on[0][value][date]');
$this
->assertSession()
->FieldNotExists('unpublish_on[0][value][date]');
$edit = [
'fields[publish_on][region]' => 'hidden',
'fields[unpublish_on][region]' => 'hidden',
];
$this
->drupalPostForm('admin/structure/types/manage/' . $this->type . '/form-display', $edit, 'Save');
$this
->drupalGet('node/add/' . $this->type);
$assert
->elementNotExists('xpath', '//div[contains(@class, "form-type-vertical-tabs")]//details[@id = "edit-scheduler-settings"]');
$this
->assertSession()
->FieldNotExists('publish_on[0][value][date]');
$this
->assertSession()
->FieldNotExists('unpublish_on[0][value][date]');
}
public function testHideSeconds() {
$this
->drupalLogin($this->schedulerUser);
$config = $this
->config('scheduler.settings');
$this
->drupalGet('node/add/' . $this->type);
$publish_time_field = $this
->xpath('//input[@id="edit-publish-on-0-value-time"]');
$unpublish_time_field = $this
->xpath('//input[@id="edit-unpublish-on-0-value-time"]');
$this
->assertEquals(1, $publish_time_field[0]
->getAttribute('step'), 'The input time step for publish-on is 1, so the seconds will be visible and usable.');
$this
->assertEquals(1, $unpublish_time_field[0]
->getAttribute('step'), 'The input time step for unpublish-on is 1, so the seconds will be visible and usable.');
$config
->set('hide_seconds', TRUE)
->save();
$this
->drupalGet('node/add/' . $this->type);
$publish_time_field = $this
->xpath('//input[@id="edit-publish-on-0-value-time"]');
$unpublish_time_field = $this
->xpath('//input[@id="edit-unpublish-on-0-value-time"]');
$this
->assertEquals(60, $publish_time_field[0]
->getAttribute('step'), 'The input time step for publish-on is 60, so the seconds will be hidden and not usable.');
$this
->assertEquals(60, $unpublish_time_field[0]
->getAttribute('step'), 'The input time step for unpublish-on is 60, so the seconds will be hidden and not usable.');
$edit = [
'title[0][value]' => 'Hide the seconds',
'body[0][value]' => $this
->randomString(30),
'publish_on[0][value][date]' => date('Y-m-d', strtotime('+1 day', $this->requestTime)),
'publish_on[0][value][time]' => '01:02:03',
'unpublish_on[0][value][date]' => date('Y-m-d', strtotime('+1 day', $this->requestTime)),
'unpublish_on[0][value][time]' => '04:05:06',
];
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle('Hide the seconds');
$this
->drupalGet("node/{$node->id()}/edit");
$this
->assertSession()
->FieldValueEquals('publish_on[0][value][time]', '01:02:00');
$this
->assertSession()
->FieldValueEquals('unpublish_on[0][value][time]', '04:05:00');
}
}