View source
<?php
namespace Drupal\Tests\scheduler\Functional;
use Drupal\Core\Url;
class SchedulerFieldsDisplayTest extends SchedulerBrowserTestBase {
protected static $modules = [
'field_ui',
];
protected function setUp() : void {
parent::setUp();
$this
->addPermissionsToUser($this->adminUser, [
'administer node form display',
'administer media form display',
'administer commerce_product form display',
]);
}
public function testVerticalTabOrFieldset($entityTypeId, $bundle) {
$this
->drupalLogin($this->adminUser);
$entityType = $this
->entityTypeObject($entityTypeId, $bundle);
$assert = $this
->assertSession();
$vertical_tab_xpath = '//div[contains(@class, "form-type-vertical-tabs") or contains(@class, "-secondary")]//details[@id = "edit-scheduler-settings"]';
$details_open_xpath = '//details[@id = "edit-scheduler-settings" and @open = "open"]';
$details_closed_xpath = '//details[@id = "edit-scheduler-settings" and not(@open = "open")]';
$add_url = $this
->entityAddUrl($entityTypeId, $bundle);
$this
->drupalGet($add_url);
$assert
->elementExists('xpath', $vertical_tab_xpath);
$assert
->elementExists('xpath', $details_closed_xpath);
$entityType
->setThirdPartySetting('scheduler', 'fields_display_mode', 'fieldset')
->save();
$this
->drupalGet($add_url);
$assert
->elementNotExists('xpath', $vertical_tab_xpath);
$assert
->elementExists('xpath', $details_closed_xpath);
$entityType
->setThirdPartySetting('scheduler', 'publish_required', TRUE)
->save();
$this
->drupalGet($add_url);
$assert
->elementExists('xpath', $details_open_xpath);
$entityType
->setThirdPartySetting('scheduler', 'publish_required', FALSE)
->setThirdPartySetting('scheduler', 'unpublish_required', TRUE)
->save();
$this
->drupalGet($add_url);
$assert
->elementExists('xpath', $details_open_xpath);
$entityType
->setThirdPartySetting('scheduler', 'publish_required', FALSE)
->setThirdPartySetting('scheduler', 'unpublish_required', FALSE)
->setThirdPartySetting('scheduler', 'expand_fieldset', 'always')
->save();
$this
->drupalGet($add_url);
$assert
->elementExists('xpath', $details_open_xpath);
$entityType
->setThirdPartySetting('scheduler', 'expand_fieldset', 'when_required')
->save();
$options = [
'title' => 'Contains Publish-on date ' . $this
->randomMachineName(10),
'publish_on' => strtotime('+1 day'),
];
$entity = $this
->createEntity($entityTypeId, $bundle, $options);
$this
->drupalGet($entity
->toUrl('edit-form'));
$assert
->elementExists('xpath', $details_open_xpath);
$options = [
'title' => 'Contains Publish-on date with timestamp value zero - ' . $this
->randomMachineName(10),
'publish_on' => 0,
];
$entity = $this
->createEntity($entityTypeId, $bundle, $options);
$this
->drupalGet($entity
->toUrl('edit-form'));
$assert
->elementExists('xpath', $details_open_xpath);
$options = [
'title' => 'Contains Unpublish-on date ' . $this
->randomMachineName(10),
'unpublish_on' => strtotime('+1 day'),
];
$entity = $this
->createEntity($entityTypeId, $bundle, $options);
$this
->drupalGet($entity
->toUrl('edit-form'));
$assert
->elementExists('xpath', $details_open_xpath);
$options = [
'title' => 'Contains Unpublish-on date with timestamp value zero - ' . $this
->randomMachineName(10),
'unpublish_on' => 0,
];
$entity = $this
->createEntity($entityTypeId, $bundle, $options);
$this
->drupalGet($entity
->toUrl('edit-form'));
$assert
->elementExists('xpath', $details_open_xpath);
$entityType
->setThirdPartySetting('scheduler', 'fields_display_mode', 'vertical_tab')
->save();
$this
->drupalGet($entity
->toUrl('edit-form'));
$assert
->elementExists('xpath', $vertical_tab_xpath);
$assert
->elementExists('xpath', $details_open_xpath);
}
public function testManageFormDisplay($entityTypeId, $bundle) {
$this
->drupalLogin($this->adminUser);
$entityType = $this
->entityTypeObject($entityTypeId, $bundle);
$form_display_url = Url::fromRoute("entity.entity_form_display.{$entityTypeId}.default", [
$entityType
->getEntityTypeId() => $bundle,
]);
$this
->drupalGet($form_display_url);
$this
->assertSession()
->fieldExists('edit-fields-scheduler-settings-weight');
$this
->entityTypeObject($entityTypeId, $bundle)
->setThirdPartySetting('scheduler', 'publish_enable', FALSE)
->setThirdPartySetting('scheduler', 'unpublish_enable', FALSE)
->save();
$this
->drupalGet($form_display_url);
$this
->assertSession()
->pageTextContains('Manage form display');
$this
->assertSession()
->FieldNotExists('edit-fields-scheduler-settings-weight');
}
public function testDisabledFields($entityTypeId, $bundle) {
$this
->drupalLogin($this->adminUser);
$entityType = $this
->entityTypeObject($entityTypeId, $bundle);
$assert = $this
->assertSession();
$edit = [
'fields[publish_on][region]' => 'hidden',
];
$form_display_url = Url::fromRoute("entity.entity_form_display.{$entityTypeId}.default", [
$entityType
->getEntityTypeId() => $bundle,
]);
$this
->drupalGet($form_display_url);
$this
->submitForm($edit, 'Save');
$add_url = $this
->entityAddUrl($entityTypeId, $bundle);
$this
->drupalGet($add_url);
$assert
->elementExists('xpath', '//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
->drupalGet($form_display_url);
$this
->submitForm($edit, 'Save');
$this
->drupalGet($add_url);
$assert
->elementExists('xpath', '//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
->drupalGet($form_display_url);
$this
->submitForm($edit, 'Save');
$this
->drupalGet($add_url);
$assert
->elementNotExists('xpath', '//details[@id = "edit-scheduler-settings"]');
$this
->assertSession()
->FieldNotExists('publish_on[0][value][date]');
$this
->assertSession()
->FieldNotExists('unpublish_on[0][value][date]');
}
public function testHideSeconds($entityTypeId, $bundle) {
$this
->drupalLogin($this->schedulerUser);
$config = $this
->config('scheduler.settings');
$titleField = $entityTypeId == 'media' ? 'name' : 'title';
$add_url = $this
->entityAddUrl($entityTypeId, $bundle);
$this
->drupalGet($add_url);
$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($add_url);
$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 = [
"{$titleField}[0][value]" => 'Hide the seconds',
'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');
$entity = $this
->getEntityByTitle($entityTypeId, 'Hide the seconds');
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->assertSession()
->FieldValueEquals('publish_on[0][value][time]', '01:02:00');
$this
->assertSession()
->FieldValueEquals('unpublish_on[0][value][time]', '04:05:00');
}
}