1 node.test NodeTypeTestCase::testNodeTypeCreation()

Tests creating a content type programmatically and via a form.

File

core/modules/node/tests/node.test, line 1990
Tests for node.module.

Class

NodeTypeTestCase
Tests related to node types.

Code

function testNodeTypeCreation() {
  // Ensure node type functions (node_type_get_*) work correctly.
  $node_types = node_type_get_types();
  $node_names = node_type_get_names();

  $this->assertTrue(isset($node_types['post']), 'Node type post is available.');
  $this->assertTrue(isset($node_types['page']), 'Node type page is available.');

  $this->assertEqual($node_types['post']->name, $node_names['post'], 'Correct node type base has been returned.');

  $this->assertEqual($node_types['post'], node_type_get_type('post'), 'Correct node type has been returned.');
  $this->assertEqual($node_types['post']->name, node_type_get_name('post'), 'Correct node type name has been returned.');
  $this->assertEqual($node_types['page']->base, node_type_get_base('page'), 'Correct node type base has been returned.');

  // Create a content type programmatically.
  $type = $this->backdropCreateContentType();

  $config = config('node.type.' . $type->type);
  $this->assertFalse($config->isNew(), 'The new content type has been created in config.');

  // Login a test user.
  $web_user = $this->backdropCreateUser(array('create ' . $type->name . ' content'));
  $this->backdropLogin($web_user);

  $this->backdropGet('node/add/' . str_replace('_', '-', $type->name));
  $this->assertResponse(200, 'The new content type can be accessed at node/add.');

  // Create a content type via the user interface.
  $web_user = $this->backdropCreateUser(array('bypass node access', 'administer content types', 'administer fields'));
  $this->backdropLogin($web_user);
  $edit = array(
    'name' => 'foo',
    'title_label' => 'title for foo',
    'type' => 'foo',
  );
  $this->backdropPost('admin/structure/types/add', $edit, t('Save content type'));
  $config = config('node.type.foo');
  $this->assertFalse($config->isNew(), 'The new content type has been created in config.');
  $this->assertEqual($config->get('settings.hidden_path'), '0', 'The new content type has hidden_path disabled.');

  // Add a node of type foo; make sure we can visit it with 200 response..
  $foo_settings = array('uid' => $web_user->uid, 'type' => 'foo');
  $foo_node = $this->backdropCreateNode($foo_settings);

  $web_user = $this->backdropCreateUser(array('search content', 'use advanced search'));
  $this->backdropLogin($web_user);

  $this->backdropGet('/node/' . $foo_node->nid);
  $this->assertResponse(200, 'Received 200 response from node type foo with hidden_path disabled.');

  // Check that node is listed in /search.
  $this->cronRun();

  // Search for node title. Should be one result.
  $this->backdropPost('search/node', array('keys' => $foo_node->title), t('Search'));
  $this->assertText($foo_node->title, 'Node found in search results.');

  // Ensure content type is available as option.
  $this->assertFieldByXPath("//input[@name='type[foo]']", 'foo', 'Content type of foo is available as filter for search.');

  // Edit content type foo to hidden_path = 1 and check the setting.
  $config->set('settings.hidden_path', '1');
  $config->save();
  $this->assertEqual($config->get('settings.hidden_path'), '1', 'The new content type has hidden_path enabled.');
  $foo_node2 = $this->backdropCreateNode($foo_settings);
  backdrop_flush_all_caches();
  $this->backdropGet('/node/' . $foo_node2->nid);
  $this->assertResponse(404, 'Received 404 response from node type foo where user restricted from viewing full page.');

  // Check that node is not listed in /search.
  $this->cronRun();

  // Search for node title. Should be zero results.
  $this->backdropPost('search/node', array('keys' => $foo_node->title), t('Search'));
  $this->assertNoText($foo_node->title, 'Node not found in search results.');

  // Ensure content type is not available as option.
  $this->assertNoFieldByXPath("//input[@name='type[foo]']", 'foo', 'Content type of foo is not available as filter for search.');

  // Ensure publishing settings are loaded correctly even when using boolean
  // values for "status_default".
  $type->settings['status_default'] = TRUE;
  node_type_save($type);
  $type = node_type_load($type->type);
  // The status should be NODE_PUBLISHED (1) not the Boolean (TRUE).
  $this->assertIdentical($type->settings['status_default'], NODE_PUBLISHED);
  $this->assertNotIdentical($type->settings['status_default'], TRUE);
}