public function PublicationDateTest::testActionSavingOnForms in Publication Date 8.2
Same name and namespace in other branches
- 8 src/Tests/PublicationDateTest.php \Drupal\publication_date\Tests\PublicationDateTest::testActionSavingOnForms()
Test automatic saving of variables via forms
File
- tests/
src/ Functional/ PublicationDateTest.php, line 121 - Contains \Drupal\publication_date\Tests\PublicationDateTest.
Class
- PublicationDateTest
- Tests for publication_date.
Namespace
Drupal\publication_date\TestsCode
public function testActionSavingOnForms() {
$edit = array();
$edit["title[0][value]"] = 'publication test node ' . $this
->randomMachineName(10);
$edit['status[value]'] = 1;
// Hard to test created time == REQUEST_TIME because simpletest launches a
// new HTTP session, so just check it's set.
$this
->drupalPostForm('node/add/page', $edit, (string) t('Save'));
$node = $this
->drupalGetNodeByTitle($edit["title[0][value]"]);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$value = $this
->getPubdateFieldValue();
list($date, $time) = explode(' ', $value);
// Make sure it was created with Published At set.
$this
->assertNotNull($value, t('Publication date set initially'));
// Unpublish the node and check that the field value is maintained.
$edit['status[value]'] = 0;
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, (string) t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertFieldByName('published_at[0][value][date]', $date, t('Pubdate is maintained when unpublished'));
$this
->assertFieldByName('published_at[0][value][time]', $time, t('Pubdate is maintained when unpublished'));
// Republish the node and check that the field value is maintained.
$edit['status[value]'] = 1;
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, (string) t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertFieldByName('published_at[0][value][date]', $date, t('Pubdate is maintained when republished'));
$this
->assertFieldByName('published_at[0][value][time]', $time, t('Pubdate is maintained when republished'));
// Set a custom time and make sure that it is stored correctly.
$ctime = REQUEST_TIME - 180;
$edit['published_at[0][value][date]'] = \Drupal::service('date.formatter')
->format($ctime, 'custom', 'Y-m-d');
$edit['published_at[0][value][time]'] = \Drupal::service('date.formatter')
->format($ctime, 'custom', 'H:i:s');
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, (string) t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$value = $this
->getPubdateFieldValue();
list($date, $time) = explode(' ', $value);
$this
->assertEqual($date, \Drupal::service('date.formatter')
->format($ctime, 'custom', 'Y-m-d'), t('Custom date was set'));
$this
->assertEqual($time, \Drupal::service('date.formatter')
->format($ctime, 'custom', 'H:i:s'), t('Custom time was set'));
// Set the field to empty and and make sure the published date is reset.
$edit['published_at[0][value][date]'] = '';
$edit['published_at[0][value][time]'] = '';
sleep(2);
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, (string) t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$new_value = $this
->getPubdateFieldValue();
list($new_date, $new_time) = explode(' ', $this
->getPubdateFieldValue());
$this
->assertNotNull($new_value, t('Published time was set automatically when there was no value entered'));
$this
->assertNotEqual($new_time, $time, t('The new published-at time is different from the custom time'));
$this
->assertTrue(strtotime($this
->getPubdateFieldValue()) > strtotime($value), t('The new published-at time is greater than the original one'));
// Unpublish the node.
$edit['status[value]'] = 0;
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, (string) t('Save'));
// Set the field to empty and and make sure that it stays empty.
$edit['published_at[0][value][date]'] = '';
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, (string) t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertFieldByName('published_at[0][value][date]', '', t('Publication date field is empty'));
}