Skip to content
Snippets Groups Projects
Commit 90e8921e authored by Robert Kasza's avatar Robert Kasza
Browse files

Issue #3106997 by weekbeforenext, nitesh624: Past Date shouldn't allow in scheduling content

parent d82253f8
Branches
Tags
1 merge request!15Issue #3106997 by weekbeforenext, nitesh624: Past Date shouldn't allow in scheduling content
Pipeline #244967 passed with warnings
Showing with 121 additions and 12 deletions
time_step: 60
allow_past_dates: true
......@@ -5,3 +5,6 @@ lightning_scheduler.settings:
time_step:
type: integer
label: "The time input's step attribute"
allow_past_dates:
type: boolean
label: "Allow scheduling dates/times in the past."
......@@ -181,10 +181,18 @@ export default class extends Component
const value = dateFormat(this.state.edit.when, 'isoDate');
let mindate = "";
if (!drupalSettings.lightning_scheduler.allow_past_dates) {
let today;
today = new Date();
mindate = today.getFullYear() + '-' + ("0" + (today.getMonth() + 1)).slice(-2) + '-' + ("0" + today.getDate()).slice(-2);
}
return (
<label>
<span hidden>{ Drupal.t('Scheduled transition date') }</span>
<input required defaultValue={ value } type="date" onChange={ onChange } class="form-date form-element form-element--type-date form-element--api-date"/>
<input required min={ mindate } defaultValue={ value } type="date" onChange={ onChange } class="form-date form-element form-element--type-date form-element--api-date"/>
</label>
);
}
......
This diff is collapsed.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
......@@ -6,3 +6,17 @@
function lightning_scheduler_update_9001(&$sandbox) {
// Forcing cache clear for service argument changes.
}
/**
* Set the new config setting allow_past_dates to true.
*/
function lightning_scheduler_update_9002(&$sandbox) {
// Get the configFactory.
$config_factory = \Drupal::configFactory();
// Get the editable configuration for the lightning_scheduler module.
$lightning_scheduler_configuration = $config_factory->getEditable('lightning_scheduler.settings');
// Set allow_past_dates to TRUE for existing sites.
$lightning_scheduler_configuration->set('allow_past_dates', TRUE);
// Save the configuration changes.
$lightning_scheduler_configuration->save();
}
......@@ -49,6 +49,12 @@ final class SettingsForm extends ConfigFormBase {
'#default_value' => $this->config('lightning_scheduler.settings')->get('time_step'),
];
$form['allow_past_dates'] = [
'#type' => 'checkbox',
'#title' => $this->t("Allow scheduling dates/times in the past."),
'#default_value' => $this->config('lightning_scheduler.settings')->get('allow_past_dates'),
];
return parent::buildForm($form, $form_state);
}
......@@ -58,6 +64,7 @@ final class SettingsForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('lightning_scheduler.settings')
->set('time_step', (int) $form_state->getValue('time_step'))
->set('allow_past_dates', $form_state->getValue('allow_past_dates'))
->save();
parent::submitForm($form, $form_state);
......
......@@ -67,6 +67,11 @@ final class ModerationStateWidget extends BaseModerationStateWidget {
$latest_revision->get('scheduled_transition_state')
);
// Get the allow_past_dates configuration setting.
$allow_past_dates = \Drupal::configFactory()
->get('lightning_scheduler.settings')
->get('allow_past_dates');
$element['scheduled_transitions'] = [
'#type' => 'html_tag',
'#tag' => 'TransitionSet',
......@@ -76,6 +81,11 @@ final class ModerationStateWidget extends BaseModerationStateWidget {
],
'#attached' => [
'library' => ['lightning_scheduler/widget'],
'drupalSettings' => [
'lightning_scheduler' => [
'allow_past_dates' => $allow_past_dates,
],
],
],
'data' => [
'#type' => 'hidden',
......
......@@ -120,6 +120,10 @@ final class TransitionManager {
$minimum_date = NULL;
if (!\Drupal::config('lightning_scheduler.settings')->get('allow_past_dates')) {
$minimum_date = \Drupal::time()->getRequestTime();
}
foreach ($data as $transition) {
if (empty($transition['when'])) {
$form_state->setError($element, t('Scheduled transitions must have a date and time.'));
......@@ -136,11 +140,9 @@ final class TransitionManager {
// The transition must take place after $minimum_date.
if (isset($minimum_date) && $transition['when'] < $minimum_date) {
$variables = [
'@date' => date('F j, Y', $minimum_date),
'@time' => date('g:i A', $minimum_date),
];
$form_state->setError($element, t('You cannot schedule a transition to take place before @time on @date.', $variables));
$form_state->setError($element, t('You cannot schedule a transition to take place before @date.', [
'@date' => self::renderTranslatableDate($minimum_date, 'long'),
]));
return;
}
$minimum_date = $transition['when'];
......@@ -249,4 +251,35 @@ final class TransitionManager {
}
}
/**
* Render a date from epoch using a translatable date format in Drupal.
*
* @param int $timestamp
* The epoch timestamp.
* @param string $format_id
* The date format ID to use.
*
* @return string
* The formatted date string.
*/
private static function renderTranslatableDate($timestamp, $format_id) {
// Load the date format storage service
$date_format_storage = \Drupal::entityTypeManager()->getStorage('date_format');
// Check if the date format ID exists
$dateFormat = $date_format_storage->load($format_id);
// Convert epoch to DateTime object
$dateTime = new \DateTime();
$dateTime->setTimestamp($timestamp);
// Load the date formatter service
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
// Render the date using the format ID
return (!empty($dateFormat)) ?
$date_formatter->format($dateTime->getTimestamp(), $format_id) :
$date_formatter->format($dateTime->getTimestamp(), 'custom', 'F j, Y g:i A');
}
}
......@@ -67,14 +67,16 @@ class TransitionTest extends WebDriverTestBase {
$this->drupalLogin($account);
$this->setUpTimeZone();
$this->setTimeStep();
$this->setAllowPastDates();
$this->drupalGet('/node/add/page');
$this->getSession()->getPage()->fillField('Title', $this->randomString());
}
public function testPublishInPast() {
public function testPublishInPastWhenItIsEnabled() {
$assert_session = $this->assertSession();
// We don't need to change that as it is enabled in the default config.
$this->createTransition('Published', $this->currentTime - 10);
$this->getSession()->getPage()->pressButton('Save');
$this->cronRun();
......@@ -83,8 +85,19 @@ class TransitionTest extends WebDriverTestBase {
$assert_session->elementNotExists('css', '.scheduled-transition');
}
public function testPublishInPastWhenItIsDisabled() {
$assert_session = $this->assertSession();
$this->setDisablePastDates();
$this->createTransition('Published', $this->currentTime - 1);
$this->getSession()->getPage()->pressButton('Save');
$assert_session->addressEquals('/node/add/page');
$assert_session->pageTextContains('You cannot schedule a transition to take place before');
}
/**
* @depends testPublishInPast
* @depends testPublishInPastWhenItIsEnabled
*/
public function testSkipInvalidTransition() {
$assert_session = $this->assertSession();
......
......@@ -40,6 +40,7 @@ class UiTest extends WebDriverTestBase {
$this->drupalPlaceBlock('local_tasks_block');
$this->setUpTimeZone();
$this->setTimeStep();
$this->setAllowPastDates();
}
public function testUiNotPresentWithoutModeration() {
......
......@@ -42,6 +42,7 @@ class UiTranslationTest extends WebDriverTestBase {
$this->drupalPlaceBlock('local_tasks_block');
$this->setUpTimeZone();
$this->setTimeStep();
$this->setAllowPastDates();
}
public function testUiTranslation() {
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\lightning_scheduler\Kernel;
use Drupal\Tests\lightning_scheduler\Traits\SchedulerUiTrait;
use Drupal\TestTools\Random;
use Prophecy\PhpUnit\ProphecyTrait;
use Drupal\Component\Serialization\Json;
......@@ -18,6 +19,8 @@ use Drupal\lightning_scheduler\TransitionManager;
class TransitionManagerTest extends KernelTestBase {
use ProphecyTrait;
use SchedulerUiTrait;
/**
* {@inheritdoc}
*/
......@@ -37,6 +40,9 @@ class TransitionManagerTest extends KernelTestBase {
* @dataProvider providerValidate
*/
public function testValidate($value, $expected_error = NULL) {
$this->setRequestTime($this->currentTime);
$this->setAllowPastDates();
$element = [
'#value' => Json::encode($value),
'#name' => 'test_element',
......@@ -141,7 +147,7 @@ class TransitionManagerTest extends KernelTestBase {
'when' => mktime(2, 30, 0, 9, 4, 2018),
],
],
"You cannot schedule a transition to take place before 3:42 PM on November 5, 2018.",
"You cannot schedule a transition to take place before November 5, 2018 3:42 PM.",
],
'valid same dates, valid times, invalid order' => [
[
......@@ -154,7 +160,7 @@ class TransitionManagerTest extends KernelTestBase {
'when' => mktime(4, 46, 0, 9, 19, 2022),
],
],
"You cannot schedule a transition to take place before 6:30 AM on September 19, 2022.",
"You cannot schedule a transition to take place before September 19, 2022 6:30 AM.",
],
'valid different dates' => [
[
......
......@@ -107,6 +107,18 @@ trait SchedulerUiTrait {
->save();
}
protected function setAllowPastDates() {
$this->config('lightning_scheduler.settings')
->set('allow_past_dates', TRUE)
->save();
}
protected function setDisablePastDates() {
$this->config('lightning_scheduler.settings')
->set('allow_past_dates', FALSE)
->save();
}
/**
* Sets the time of the request, according to the datetime.time service.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment