node_example.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @file
  4. * Simpletest case for node_example module.
  5. *
  6. * Verify example module functionality.
  7. */
  8. /**
  9. * Functionality tests for node example module.
  10. *
  11. * @ingroup node_example
  12. */
  13. class NodeExampleTestCase extends DrupalWebTestCase {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public static function getInfo() {
  18. return array(
  19. 'name' => 'Node example',
  20. 'description' => 'Verify the custom node type creation.',
  21. 'group' => 'Examples',
  22. );
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function setUp() {
  28. // Enable the module.
  29. parent::setUp('node_example');
  30. }
  31. /**
  32. * API-level content type test.
  33. *
  34. * This test will verify that when the module is installed, it:
  35. * - Adds a new content type, node_example.
  36. * - Attaches a body field.
  37. * - Attaches three other fields.
  38. * - Creates a view mode, example_node_list.
  39. */
  40. public function testInstallationApi() {
  41. // At this point, the module should be installed.
  42. // First check for our content type.
  43. $node_type = node_type_get_type('node_example');
  44. $this->assertTrue($node_type, 'Node Example Type was created.', 'API');
  45. // How about the body field?
  46. $body = field_info_instance('node', 'body', 'node_example');
  47. $this->assertTrue($body, 'Node Example Type has a body field.', 'API');
  48. // Now look for our attached fields.
  49. // We made a handy function that tells us...
  50. $attached_fields = _node_example_installed_instances();
  51. foreach ($attached_fields as $field_name => $field_info) {
  52. $field = field_info_instance('node', $field_name, 'node_example');
  53. $this->assertTrue($field,
  54. 'Field: ' . $field_name . ' was attached to node_example.', 'API');
  55. }
  56. // And that view mode...
  57. // entity_get_info() invokes hook_entity_info_alter(), so it's
  58. // a good place to verify that our code works.
  59. $entities = entity_get_info('node');
  60. $this->assertTrue(isset($entities['view modes']['example_node_list']),
  61. 'Added example_node_list view mode.', 'API');
  62. }
  63. /**
  64. * Verify the functionality of the example module.
  65. */
  66. public function testNodeCreation() {
  67. // Create and login user.
  68. $account = $this->drupalCreateUser(array('access content', 'create node_example content'));
  69. $this->drupalLogin($account);
  70. // Create a new node. The image makes it more complicated, so skip it.
  71. $edit = array(
  72. 'title' => $this->randomName(),
  73. 'node_example_color[und][0][value]' => 'red',
  74. 'node_example_color[und][1][value]' => 'green',
  75. 'node_example_color[und][2][value]' => 'blue',
  76. 'node_example_quantity[und][0][value]' => 100,
  77. );
  78. $this->drupalPost('node/add/node-example', $edit, t('Save'));
  79. $this->assertText("Example Node Type " . $edit['title'] . " has been created", "Found node creation message");
  80. $this->assertPattern("/The colors available.*red.*green.*blue/", "Correct 'colors available' on node page");
  81. // Look on the examples page to make sure it shows up there also.
  82. $this->drupalGet('examples/node_example');
  83. $this->assertText($edit['title'], "Found random title string");
  84. $this->assertPattern("/red.*green.*blue/", "Correct 'colors available' on node example page");
  85. }
  86. /**
  87. * Check the value of body label.
  88. *
  89. * Checks whether body label has a value of "Example Description"
  90. */
  91. public function testBodyLabel() {
  92. // Create and login user.
  93. $account = $this->drupalCreateUser(array('access content', 'create node_example content'));
  94. $this->drupalLogin($account);
  95. // Request a node add node-example page.
  96. // Test whether the body label equals 'Example Description'.
  97. // Use '$this->assertRaw' to make certain to test the body label and not
  98. // some other text.
  99. $this->drupalGet('node/add/node-example');
  100. $this->assertResponse(200, 'node/add/node-example page found');
  101. $this->assertRaw('<label for="edit-body-und-0-value">Example Description </label>', 'Body label equals \'Example Description\'');
  102. }
  103. }