You are here

public function PublicationDateTest::testActionSaving in Publication Date 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/PublicationDateTest.php \Drupal\publication_date\Tests\PublicationDateTest::testActionSaving()

Test automatic saving of variables.

File

src/Tests/PublicationDateTest.php, line 49
Contains \Drupal\publication_date\Tests\PublicationDateTest.

Class

PublicationDateTest
Tests for publication_date.

Namespace

Drupal\publication_date\Tests

Code

public function testActionSaving() {

  // Create node to edit.
  $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');

  // Publish the node.
  $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');

  // Remember time.
  $time = $published_node->published_at->value;

  // Unpublish the node and check that the field value is maintained.
  $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');

  // Set the field to zero and and make sure the published date is empty.
  $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');

  // Set a custom time and make sure that it is saved.
  $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');

  // Republish the node and check that the field value is maintained.
  $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');

  // Set the field to zero and and make sure the published date is reset.
  $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');

  // Now try it by purely pushing the forms around.
}