public function PublicationDateTest::testActionSavingOnForms in Publication Date 8
Same name and namespace in other branches
- 8.2 tests/src/Functional/PublicationDateTest.php \Drupal\publication_date\Tests\PublicationDateTest::testActionSavingOnForms()
Test automatic saving of variables via forms
File
- src/
Tests/ PublicationDateTest.php, line 116 - Contains \Drupal\publication_date\Tests\PublicationDateTest.
Class
- PublicationDateTest
- Tests for publication_date.
Namespace
Drupal\publication_date\TestsCode
public function testActionSavingOnForms() {
$langcode = LANGUAGE_NONE;
$edit = array();
$edit["title"] = 'publication test node ' . $this
->randomName(10);
$edit["body[{$langcode}][0][value]"] = 'publication node test body ' . $this
->randomName(32) . ' ' . $this
->randomName(32);
$edit['status'] = 1;
// Hard to test created time == REQUEST_TIME because simpletest launches a
// new HTTP session, so just check it's set.
$this
->drupalPost('node/add/page', $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit['title']);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$value = $this
->_getPubdateFieldValue();
// 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'] = 0;
$this
->drupalPost('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertFieldByName('pubdate', $value, t('Pubdate is maintained when unpublished'));
// Republish the node and check that the field value is maintained.
$edit['status'] = 1;
$this
->drupalPost('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertFieldByName('pubdate', $value, t('Pubdate is maintained when republished'));
// Set a custom time and make sure that it is stored correctly.
$ctime = REQUEST_TIME - 180;
$edit['pubdate'] = format_date($ctime, 'custom', 'Y-m-d H:i:s O');
$this
->drupalPost('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$custom_value = $this
->_getPubdateFieldValue();
$this
->assertTrue($custom_value == format_date($ctime, 'custom', 'Y-m-d H:i:s O'), t('Custom time/date was set'));
// Set the field to empty and and make sure the published date is reset.
$edit['pubdate'] = '';
sleep(2);
$this
->drupalPost('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$new_value = $this
->_getPubdateFieldValue();
$this
->assertNotNull($new_value, t('Published time was set automatically when there was no value entered'));
$this
->assertNotNull($new_value != $custom_value, t('The new published-at time is different from the custom time'));
$this
->assertTrue($new_value > $value, t('The new published-at time is greater than the original one'));
// Unpublish the node.
$edit['status'] = 0;
$this
->drupalPost('node/' . $node
->id() . '/edit', $edit, t('Save'));
// Set the field to empty and and make sure that it stays empty.
$edit['pubdate'] = '';
$this
->drupalPost('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertFieldByName('pubdate', '', t('Publication date field is empty'));
}