View source
<?php
namespace Drupal\publication_date\Tests;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
class PublicationDateTest extends BrowserTestBase {
public static $modules = array(
'node',
'user',
'publication_date',
);
protected $defaultTheme = 'stark';
protected $privileged_user;
protected 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());
$value = $unpublished_node->published_at->value;
$this
->assertEqual($unpublished_node->published_at->value, PUBLICATION_DATE_DEFAULT);
$this
->assertEqual($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
->assertEqual($unpublished_node->published_at->value, PUBLICATION_DATE_DEFAULT);
$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() {
$edit = array();
$edit["title[0][value]"] = 'publication test node ' . $this
->randomMachineName(10);
$edit['status[value]'] = 1;
$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);
$this
->assertNotNull($value, t('Publication date set initially'));
$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'));
$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'));
$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'));
$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'));
$edit['status[value]'] = 0;
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, (string) t('Save'));
$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'));
}
public function testActionSavingSetDate() {
$node = $this
->drupalCreateNode(array(
'status' => 0,
));
$unpublished_node = Node::load($node
->id());
$this
->assertEqual($unpublished_node->published_at->value, PUBLICATION_DATE_DEFAULT);
$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() {
$this
->assertSession()
->fieldExists('published_at[0][value][date]');
$field = $this
->xpath('//input[@name="published_at[0][value][date]"]');
$date = (string) $field[0]
->getValue();
$this
->assertSession()
->fieldExists('published_at[0][value][time]');
$field = $this
->xpath('//input[@name="published_at[0][value][time]"]');
$time = (string) $field[0]
->getValue();
return trim($date . ' ' . $time);
}
}