dbtng_example.test 5.1 KB

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