View source
<?php
namespace Drupal\publication_date\Tests;
use Drupal\node\Entity\NodeType;
use Drupal\simpletest\WebTestBase;
class PublicationDateTest extends WebTestBase {
public static $modules = array(
'node',
'user',
'publication_date',
);
protected $privileged_user;
public function setUp() {
parent::setUp();
NodeType::create([
'type' => 'page',
'name' => 'Page',
])
->save();
$this->privileged_user = $this
->drupalCreateUser(array(
'create page content',
'edit own page content',
'administer nodes',
'set page published on date',
));
$this
->drupalLogin($this->privileged_user);
}
public function testActionSaving() {
$node = $this
->drupalCreateNode(array(
'status' => 0,
));
$unpublished_node = node_load($node
->id());
$this
->assertTrue(empty($unpublished_node->published_at->value), 'Published date is initially empty');
$this
->assertTrue($unpublished_node->published_at->published_at_or_now == REQUEST_TIME, 'Published at or now date is REQUEST_TIME');
$unpublished_node->status = 1;
$unpublished_node
->save();
$published_node = node_load($node
->id());
$this
->assertTrue(is_numeric($published_node->published_at->value), 'Published date is integer/numberic once published');
$this
->assertTrue($published_node->published_at->value == REQUEST_TIME, 'Published date is REQUEST_TIME');
$this
->assertTrue($unpublished_node->published_at->published_at_or_now == $published_node->published_at->value, 'Published at or now date equals published date');
$time = $published_node->published_at->value;
$published_node->status = 0;
$published_node
->save();
$unpublished_node = node_load($node
->id());
$this
->assertTrue($unpublished_node->published_at->value == $time, 'Published date is maintained when unpublished');
$unpublished_node->published_at->value = 0;
$unpublished_node
->save();
$unpublished_node = node_load($node
->id());
$this
->assertTrue(empty($unpublished_node->published_at->value), 'Published date is empty when reset');
$time = $unpublished_node->published_at->value = 122630400;
$unpublished_node
->save();
$unpublished_node = node_load($node
->id());
$this
->assertTrue($unpublished_node->published_at->value == $time, 'Custom published date is saved');
$this
->assertTrue($unpublished_node->published_at->published_at_or_now == $time, 'Published at or now date equals published date');
$unpublished_node->status = 1;
$unpublished_node
->save();
$published_node = node_load($node
->id());
$this
->assertTrue($published_node->published_at->value == $time, 'Custom published date is maintained when republished');
$published_node->published_at->value = 0;
$published_node
->save();
$published_node = node_load($node
->id());
$this
->assertTrue($published_node->published_at->value > $time, 'Published date is reset');
}
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;
$this
->drupalPost('node/add/page', $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit['title']);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$value = $this
->_getPubdateFieldValue();
$this
->assertNotNull($value, t('Publication date set initially'));
$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'));
$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'));
$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'));
$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'));
$edit['status'] = 0;
$this
->drupalPost('node/' . $node
->id() . '/edit', $edit, t('Save'));
$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'));
}
public function testActionSavingSetDate() {
$node = $this
->drupalCreateNode(array(
'status' => 0,
));
$unpublished_node = node_load($node
->id());
$this
->assertTrue(empty($unpublished_node->published_at->value), 'Published date is initially empty');
$unpublished_node->status = 1;
$static_time = 12345678;
$unpublished_node->published_at->value = $static_time;
$unpublished_node
->save();
$published_node = node_load($node
->id());
$this
->assertTrue(is_numeric($published_node->published_at->value), 'Published date is integer/numberic once published');
$this
->assertTrue($published_node->published_at->value == $static_time, 'Published date is set to what we expected');
}
private function _getPubdateFieldValue() {
$value = '';
if ($this
->assertField('pubdate', t('Published At field exists'))) {
$field = $this
->xpath('//input[@id="edit-pubdate"]');
$value = (string) $field[0]['value'];
return $value;
}
return $value;
}
}