dbtng_example.test

  1. examples
    1. 7 dbtng_example/dbtng_example.test
    2. 8 dbtng_example/dbtng_example.test

SimpleTests for dbtng_example module.

Classes

NameDescription
DBTNGExampleUnitTestCaseDefault test case for the dbtng_example module.

File

dbtng_example/dbtng_example.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * SimpleTests for dbtng_example module.
  5. */
  6. /**
  7. * Default test case for the dbtng_example module.
  8. */
  9. class DBTNGExampleUnitTestCase extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'DBTNG example unit and UI tests',
  13. 'description' => 'Various unit tests on the dbtng example module.' ,
  14. 'group' => 'Examples',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('dbtng_example');
  19. }
  20. /**
  21. * Test default module installation, two entries in the database table.
  22. */
  23. function testInstall() {
  24. $result = dbtng_example_entry_load();
  25. $this->assertEqual(
  26. count($result),
  27. 2,
  28. t('Found two entries in the table after installing the module.')
  29. );
  30. }
  31. /**
  32. * Test the UI.
  33. */
  34. function testUI() {
  35. // Test the basic list.
  36. $this->drupalGet('examples/dbtng');
  37. $this->assertPattern("/John[td\/<>\w]+Doe/", t("Text 'John Doe' found in table"));
  38. //Test the add tab.
  39. // Add the new entry.
  40. $this->drupalPost('examples/dbtng/add',
  41. array('name' => 'Some', 'surname' => 'Anonymous', 'age' => 33), t('Add'));
  42. // Now find the new entry.
  43. $this->drupalGet('examples/dbtng');
  44. $this->assertPattern("/Some[td\/<>\w]+Anonymous/", t("Text 'Some Anonymous' found in table"));
  45. // Try the update tab.
  46. // Find out the pid of our "anonymous" guy.
  47. $result = dbtng_example_entry_load(array('surname' => 'Anonymous'));
  48. $this->drupalGet("examples/dbtng");
  49. $this->assertEqual(
  50. count($result),
  51. 1,
  52. t('Found one entry in the table with surname = "Anonymous".')
  53. );
  54. $entry = $result[0];
  55. unset($entry->uid);
  56. $entry->name = 'NewFirstName';
  57. $this->drupalPost('examples/dbtng/update',
  58. (array)$entry, t('Update'));
  59. // Now find the new entry.
  60. $this->drupalGet('examples/dbtng');
  61. $this->assertPattern("/NewFirstName[td\/<>\w]+Anonymous/", t("Text 'NewFirstName Anonymous' found in table"));
  62. // Try the advanced tab.
  63. $this->drupalGet('examples/dbtng/advanced');
  64. $rows = $this->xpath("//*[@id='block-system-main']/div/table[1]/tbody/tr");
  65. $this->assertEqual(count($rows), 1, t("One row found in advanced view"));
  66. $this->assertFieldByXPath("//*[@id='block-system-main']/div/table[1]/tbody/tr/td[4]", "Roe", "Name 'Roe' Exists in advanced list");
  67. }
  68. /**
  69. * Test several combinations, adding entries, updating and deleting.
  70. */
  71. function testAPIExamples() {
  72. // Create a new entry.
  73. $entry = array(
  74. 'name' => 'James',
  75. 'surname' => 'Doe',
  76. 'age' => 23,
  77. );
  78. dbtng_example_entry_insert($entry);
  79. // Save another entry
  80. $entry = array(
  81. 'name' => 'Jane',
  82. 'surname' => 'NotDoe',
  83. 'age' => 19,
  84. );
  85. dbtng_example_entry_insert($entry);
  86. // Verify that 4 records are found in the database
  87. $result = dbtng_example_entry_load();
  88. $this->assertEqual(
  89. count($result),
  90. 4,
  91. t('Found a total of four entries in the table after creating two additional entries.')
  92. );
  93. // Verify 2 of these records have 'Doe' as surname
  94. $result = dbtng_example_entry_load(array('surname' => 'Doe'));
  95. $this->assertEqual(
  96. count($result),
  97. 2,
  98. t('Found two entries in the table with surname = "Doe".')
  99. );
  100. // Now find our not-Doe entry.
  101. $result = dbtng_example_entry_load(array('surname' => 'NotDoe'));
  102. $this->assertEqual(
  103. count($result),
  104. 1,
  105. t('Found one entry in the table with surname "NotDoe'));
  106. // Our NotDoe will be changed to "NowDoe".
  107. $entry = $result[0];
  108. $entry->surname = "NowDoe";
  109. dbtng_example_entry_update((array)$entry);
  110. $result = dbtng_example_entry_load(array('surname' => 'NowDoe'));
  111. $this->assertEqual(
  112. count($result),
  113. 1,
  114. t("Found renamed 'NowDoe' surname"));
  115. // Read only John Doe entry.
  116. $result = dbtng_example_entry_load(array('name' => 'John', 'surname' => 'Doe'));
  117. $this->assertEqual(
  118. count($result),
  119. 1,
  120. t('Found one entry for John Doe.')
  121. );
  122. // Get the entry
  123. $entry = (array) end($result);
  124. // Change age to 45
  125. $entry['age'] = 45;
  126. // Update entry in database
  127. dbtng_example_entry_update((array)$entry);
  128. // Find entries with age = 45
  129. // Read only John Doe entry.
  130. $result = dbtng_example_entry_load(array('surname' => 'NowDoe'));
  131. $this->assertEqual(
  132. count($result),
  133. 1,
  134. t('Found one entry with surname = Nowdoe.')
  135. );
  136. // Verify it is Jane NowDoe.
  137. $entry = (array) end($result);
  138. $this->assertEqual(
  139. $entry['name'],
  140. 'Jane',
  141. t('The name Jane is found in the entry')
  142. );
  143. $this->assertEqual(
  144. $entry['surname'],
  145. 'NowDoe',
  146. t('The surname NowDoe is found in the entry')
  147. );
  148. // Delete the entry.
  149. dbtng_example_entry_delete($entry);
  150. // Verify that now there are only 3 records
  151. $result = dbtng_example_entry_load();
  152. $this->assertEqual(
  153. count($result),
  154. 3,
  155. t('Found only three records, a record was deleted.')
  156. );
  157. }
  158. }
Login or register to post comments