function NodeCreationTest::testAuthoredDate

Same name and namespace in other branches
  1. 8.9.x core/modules/node/tests/src/Functional/NodeCreationTest.php \Drupal\Tests\node\Functional\NodeCreationTest::testAuthoredDate()
  2. 10 core/modules/node/tests/src/Functional/NodeCreationTest.php \Drupal\Tests\node\Functional\NodeCreationTest::testAuthoredDate()
  3. 11.x core/modules/node/tests/src/Functional/NodeCreationTest.php \Drupal\Tests\node\Functional\NodeCreationTest::testAuthoredDate()

Creates nodes with different authored dates.

File

core/modules/node/tests/src/Functional/NodeCreationTest.php, line 190

Class

NodeCreationTest
Create a node and test saving it.

Namespace

Drupal\Tests\node\Functional

Code

public function testAuthoredDate() {
    $now = \Drupal::time()->getRequestTime();
    $admin = $this->drupalCreateUser([], NULL, TRUE);
    $this->drupalLogin($admin);
    // Create a node with the default creation date.
    $edit = [
        'title[0][value]' => $this->randomMachineName(8),
        'body[0][value]' => $this->randomMachineName(16),
    ];
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $this->assertNotNull($node->getCreatedTime());
    // Create a node with the custom creation date in the past.
    $date = $now - 86400;
    $edit = [
        'title[0][value]' => $this->randomMachineName(8),
        'body[0][value]' => $this->randomMachineName(16),
        'created[0][value][date]' => date('Y-m-d', $date),
        'created[0][value][time]' => date('H:i:s', $date),
    ];
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $this->assertEquals($date, $node->getCreatedTime());
    // Create a node with the custom creation date in the future.
    $date = $now + 86400;
    $edit = [
        'title[0][value]' => $this->randomMachineName(8),
        'body[0][value]' => $this->randomMachineName(16),
        'created[0][value][date]' => date('Y-m-d', $date),
        'created[0][value][time]' => date('H:i:s', $date),
    ];
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $this->assertEquals($date, $node->getCreatedTime());
    // Test an invalid date.
    $edit = [
        'title[0][value]' => $this->randomMachineName(8),
        'body[0][value]' => $this->randomMachineName(16),
        'created[0][value][date]' => '2013-13-13',
        'created[0][value][time]' => '11:00:00',
    ];
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('The Authored on date is invalid.');
    $this->assertFalse($this->drupalGetNodeByTitle($edit['title[0][value]']));
    // Test an invalid time.
    $edit = [
        'title[0][value]' => $this->randomMachineName(8),
        'body[0][value]' => $this->randomMachineName(16),
        'created[0][value][date]' => '2012-01-01',
        'created[0][value][time]' => '30:00:00',
    ];
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('The Authored on date is invalid.');
    $this->assertFalse($this->drupalGetNodeByTitle($edit['title[0][value]']));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.