NodeCreationTest.php 4.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Definition of Drupal\node\Tests\NodeCreationTest.
 */

namespace Drupal\node\Tests;

use Drupal\Core\Database\Database;
11
use Drupal\Core\Language\Language;
12

13 14 15
/**
 * Tests creating and saving a node.
 */
16
class NodeCreationTest extends NodeTestBase {
17 18 19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * Enable dummy module that implements hook_node_insert() for exceptions.
   *
   * @var array
   */
25
  public static $modules = array('node_test_exception', 'dblog', 'test_page_test');
26

27 28 29 30 31 32 33 34 35
  public static function getInfo() {
    return array(
      'name' => 'Node creation',
      'description' => 'Create a node and test saving it.',
      'group' => 'Node',
    );
  }

  function setUp() {
36
    parent::setUp();
37 38 39 40 41 42

    $web_user = $this->drupalCreateUser(array('create page content', 'edit own page content'));
    $this->drupalLogin($web_user);
  }

  /**
43
   * Creates a "Basic page" node and verifies its consistency in the database.
44 45 46 47 48
   */
  function testNodeCreation() {
    // Create a node.
    $edit = array();
    $edit["title"] = $this->randomName(8);
49
    $edit["body[0][value]"] = $this->randomName(16);
50
    $this->drupalPostForm('node/add/page', $edit, t('Save'));
51 52

    // Check that the Basic page has been created.
53
    $this->assertRaw(t('!post %title has been created.', array('!post' => 'Basic page', '%title' => $edit["title"])), 'Basic page created.');
54 55 56

    // Check that the node exists in the database.
    $node = $this->drupalGetNodeByTitle($edit["title"]);
57
    $this->assertTrue($node, 'Node found in database.');
58 59 60
  }

  /**
61
   * Verifies that a transaction rolls back the failed creation.
62 63 64 65
   */
  function testFailedPageCreation() {
    // Create a node.
    $edit = array(
66
      'uid'      => $this->loggedInUser->id(),
67 68
      'name'     => $this->loggedInUser->name,
      'type'     => 'page',
69
      'langcode' => Language::LANGCODE_NOT_SPECIFIED,
70 71 72 73 74 75 76
      'title'    => 'testing_transaction_exception',
    );

    try {
      entity_create('node', $edit)->save();
      $this->fail(t('Expected exception has not been thrown.'));
    }
77
    catch (\Exception $e) {
78 79 80 81 82 83
      $this->pass(t('Expected exception has been thrown.'));
    }

    if (Database::getConnection()->supportsTransactions()) {
      // Check that the node does not exist in the database.
      $node = $this->drupalGetNodeByTitle($edit['title']);
84
      $this->assertFalse($node, 'Transactions supported, and node not found in database.');
85 86 87 88
    }
    else {
      // Check that the node exists in the database.
      $node = $this->drupalGetNodeByTitle($edit['title']);
89
      $this->assertTrue($node, 'Transactions not supported, and node found in database.');
90 91 92

      // Check that the failed rollback was logged.
      $records = db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll();
93
      $this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
94 95 96 97
    }

    // Check that the rollback error was logged.
    $records = db_query("SELECT wid FROM {watchdog} WHERE variables LIKE '%Test exception for rollback.%'")->fetchAll();
98
    $this->assertTrue(count($records) > 0, 'Rollback explanatory error logged to watchdog.');
99
  }
100 101

  /**
102
   * Creates an unpublished node and confirms correct redirect behavior.
103 104
   */
  function testUnpublishedNodeCreation() {
105
    // Set the front page to the test page.
106
    \Drupal::config('system.site')->set('page.front', 'test-page')->save();
107

108
    // Set "Basic page" content type to be unpublished by default.
109
    \Drupal::config('node.type.page')->set('settings.node.options', array())->save();
110 111 112

    // Create a node.
    $edit = array();
113 114
    $edit['title'] = $this->randomName(8);
    $edit['body[0][value]'] = $this->randomName(16);
115
    $this->drupalPostForm('node/add/page', $edit, t('Save'));
116 117

    // Check that the user was redirected to the home page.
118 119 120 121 122
    $this->assertUrl('');
    $this->assertText(t('Test page text'));

    // Confirm that the node was created.
    $this->assertRaw(t('!post %title has been created.', array('!post' => 'Basic page', '%title' => $edit["title"])));
123
  }
124

125 126 127 128 129 130 131 132 133
  /**
   * Tests the author autocompletion textfield.
   */
  public function testAuthorAutocomplete() {
    $admin_user = $this->drupalCreateUser(array('administer nodes', 'create page content'));
    $this->drupalLogin($admin_user);

    $this->drupalGet('node/add/page');

134
    $result = $this->xpath('//input[@id="edit-name" and contains(@data-autocomplete-path, "user/autocomplete")]');
135 136 137 138 139 140 141
    $this->assertEqual(count($result), 0, 'No autocompletion without access user profiles.');

    $admin_user = $this->drupalCreateUser(array('administer nodes', 'create page content', 'access user profiles'));
    $this->drupalLogin($admin_user);

    $this->drupalGet('node/add/page');

142
    $result = $this->xpath('//input[@id="edit-name" and contains(@data-autocomplete-path, "user/autocomplete")]');
143 144 145
    $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion');
  }

146
}