faq.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * @file
  4. * Test FAQ functionality Base test class. All tests inherits this one.
  5. * Hugely based on code from the test file block.test by boombatower
  6. */
  7. /**
  8. * Base class that is extended by test cases
  9. */
  10. class FaqTestCase extends DrupalWebTestCase {
  11. protected $admin_user, $faq_user;
  12. protected $taxonomy;
  13. protected $term, $faq1, $faq2;
  14. /**
  15. * Implementation of getInfo().
  16. */
  17. public static function getInfo() {
  18. return array(
  19. 'name' => t('FAQ functionality'),
  20. 'description' => t('Base class. No tests here.'),
  21. 'group' => t('Frequently Asked Questions'),
  22. );
  23. }
  24. /**
  25. * Implementation of setUp().
  26. */
  27. function setUp() {
  28. // Install FAQ Module.
  29. parent::setUp('taxonomy', 'faq');
  30. // Create and log in user with administer taxonomy permissions.
  31. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'administer faq', 'administer faq order', 'administer blocks'));
  32. $this->drupalLogin($this->admin_user);
  33. $this->faq_user = $this->drupalCreateUser(array('create faq content', 'edit any faq content', 'delete any faq content', 'view faq page', 'access content'));
  34. // Set up the vocab and terms.
  35. $this->setupTaxonomy();
  36. // Categorize questions.
  37. $this->drupalPost('admin/config/content/faq/categories', array('faq_use_categories' => '1'), t('Save configuration'));
  38. // Set answer_user as default expert.
  39. $roles = $this->faq_user->roles;
  40. end($roles); // Set to last role (the unique one)
  41. // Start all tests logged out.
  42. $this->drupalLogout();
  43. }
  44. /**
  45. * Tear the whole thing down again
  46. */
  47. function tearDown() {
  48. // Things to tidy up like vars and stuff
  49. parent::tearDown();
  50. }
  51. /**
  52. * Generates a random string of ASCII numeric characters (values 48 to 57).
  53. *
  54. * @param $length
  55. * Length of random string to generate .
  56. * @return
  57. * Randomly generated string.
  58. */
  59. protected static function randomNumber($length = 8) {
  60. $str = '';
  61. for ($i = 0; $i < $length; $i++) {
  62. $str .= chr(mt_rand(48, 57));
  63. }
  64. return $str;
  65. }
  66. /**
  67. * Verify that current user has no access to page.
  68. *
  69. * @param $url
  70. * URL to verify.
  71. */
  72. function faqVerifyNoAccess($url) {
  73. // Test that page returns 403 Access Denied
  74. $this->drupalGet($url);
  75. $this->assertResponse(403);
  76. }
  77. /**
  78. * Set up the taxonomy - all vocabularies and stuff
  79. * Values also stored in protected variable $tax
  80. */
  81. function setupTaxonomy() {
  82. // Create vocabulary.
  83. $this->vocabulary = $vocabulary = new stdClass();
  84. $vocabulary->name = $this->randomName(8);
  85. $vocabulary->description = $this->randomName(64);
  86. $vocabulary->machine_name = drupal_strtolower($this->randomName());
  87. $vocabulary->help = '';
  88. $vocabulary->nodes = array('faq' => 'faq');
  89. $vocabulary->weight = mt_rand(0, 10);
  90. taxonomy_vocabulary_save($vocabulary);
  91. $field = array(
  92. 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
  93. 'type' => 'taxonomy_term_reference',
  94. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  95. 'settings' => array(
  96. 'allowed_values' => array(
  97. array(
  98. 'vocabulary' => $this->vocabulary->machine_name,
  99. 'parent' => 0,
  100. ),
  101. ),
  102. ),
  103. );
  104. field_create_field($field);
  105. $this->instance = array(
  106. 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
  107. 'bundle' => 'faq',
  108. 'entity_type' => 'node',
  109. 'widget' => array(
  110. 'type' => 'taxonomy_autocomplete',
  111. ),
  112. 'display' => array(
  113. 'default' => array(
  114. 'type' => 'taxonomy_term_reference_link',
  115. ),
  116. ),
  117. );
  118. field_create_instance($this->instance);
  119. // Add term
  120. // Click the last occurrence of the link.
  121. $this->term = $term = new stdClass();
  122. $term->name = $this->randomName(8);
  123. $term->description = $this->randomName(64);
  124. // Use the first available text format.
  125. $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
  126. $term->vid = $vocabulary->vid;
  127. taxonomy_term_save($term);
  128. }
  129. /**
  130. * Test accessing the FAQ page
  131. */
  132. }
  133. class FaqAccessTestClass extends FaqTestCase {
  134. /**
  135. * Implementation of getInfo().
  136. */
  137. public static function getInfo() {
  138. return array(
  139. 'name' => t('Access to FAQ pages'),
  140. 'description' => t('Access to pages by anonymous user and logged in user with rights.'),
  141. 'group' => t('Frequently Asked Questions'),
  142. );
  143. }
  144. function testFaqAccess() {
  145. // Verify that anonymous user has no access to the faq page
  146. $this->faqVerifyNoAccess('faq-page');
  147. // Create and login user
  148. $normal_user = $this->drupalCreateUser();
  149. $this->drupalLogin($normal_user);
  150. // Verify that logged in user has no access to the faq page
  151. $this->faqVerifyNoAccess('faq-page');
  152. $this->drupalLogout();
  153. $view_faq_user = $this->drupalCreateUser(array('view faq page'));
  154. $this->drupalLogin($view_faq_user);
  155. // Verify that the faq page is visible and available but empty
  156. $this->drupalGet('faq-page');
  157. $this->assertText(t('Frequently Asked Questions'), t('FAQ page is available for view faq page permissions.'));
  158. }
  159. }
  160. class CreateFaqTestCase extends FaqTestCase {
  161. /**
  162. * Implementation of getInfo().
  163. */
  164. public static function getInfo() {
  165. return array(
  166. 'name' => t('Create FAQ node'),
  167. 'description' => t('Add, edit and delete FAQ nodes when FAQ module is enabled.'),
  168. 'group' => t('Frequently Asked Questions'),
  169. );
  170. }
  171. /**
  172. * Test creating a FAQ node
  173. */
  174. function testFaqCreate() {
  175. // Create and log in user with create FAQ permissions
  176. $this->admin_user = $this->drupalCreateUser(array('create faq content', 'view faq page'));
  177. $this->drupalLogin($this->admin_user);
  178. // Verify that the faq page is visible and available but empty
  179. $this->drupalGet('faq-page');
  180. $this->assertText(t('Frequently Asked Questions'), t('FAQ page is available for view faq page permissions.'));
  181. // Fill in the Create FAQ node 1 form and post it
  182. $langcode = LANGUAGE_NONE;
  183. $this->faq1 = array();
  184. $this->faq1['title'] = 'faq1_'. $this->randomName(8);
  185. $this->faq1[$this->instance['field_name'] . '[' . $langcode .']'] = $this->term->name;
  186. $this->faq1['detailed_question'] = $this->randomName(16);
  187. $this->faq1["body[$langcode][0][value]"] = $this->randomName(16);
  188. $this->drupalPost('node/add/faq', $this->faq1, t('Save'));
  189. // Check that new FAQ node has actually been created
  190. $this->assertText(t('FAQ @title has been created.', array('@title' => $this->faq1['title'])));
  191. // Fill in the Create FAQ node 2 form and post it
  192. $this->faq2 = array();
  193. $this->faq2['title'] = 'faq2_'. $this->randomName(8);
  194. $this->faq2[$this->instance['field_name'] . '[' . $langcode .']'] = $this->randomName(8); // Add new term
  195. $this->faq2['detailed_question'] = $this->randomName(16);
  196. $this->faq2["body[$langcode][0][value]"] = $this->randomName(16);
  197. $this->drupalPost('node/add/faq', $this->faq2, t('Save'));
  198. // Check that new FAQ node has actually been created
  199. $this->assertText(t('FAQ @title has been created.', array('@title' => $this->faq2['title'])));
  200. // New user
  201. $this->drupalLogout();
  202. // Verify that logged in user has no access to the faq page
  203. $this->faqVerifyNoAccess('faq-page');
  204. // Check that the FAQ page is available and that the correct term is listed as grouping for the new FAQ node
  205. $view_faq_user = $this->drupalCreateUser(array('view faq page'));
  206. $this->drupalLogin($view_faq_user);
  207. $this->drupalGet('faq-page');
  208. $this->assertText(t('Frequently Asked Questions'), t('FAQ page is available for view faq page permissions.'));
  209. $this->assertText($this->faq1['title'], t('Created FAQ node 1 available on FAQ page.'));
  210. $this->assertText($this->faq1[$this->instance['field_name'] . '[' . $langcode .']'], t('Term for node 1 available on FAQ page.'));
  211. $this->assertText($this->faq2['title'], t('Created FAQ node 2 available on FAQ page.'));
  212. $this->assertText($this->faq2[$this->instance['field_name'] . '[' . $langcode .']'], t('Term for node 2 available on FAQ page.'));
  213. // Navigate to FAQ node created on FAQ page
  214. $this->clickLink(t($this->faq1['title']));
  215. $this->assertText(t($this->faq1["body[$langcode][0][value]"]));
  216. // Enable categorisation of FAQ nodes
  217. // Create and log in user with create and administer FAQ permissions
  218. $this->admin_user = $this->drupalCreateUser(array('create faq content', 'view faq page', 'administer faq'));
  219. $this->drupalLogin($this->admin_user);
  220. // faq_use_categories
  221. $faqcfg = array();
  222. $faqcfg['faq_use_categories'] = '1'; // Enable categorised FAQs
  223. $this->drupalPost('admin/config/content/faq/categories', $faqcfg, t('Save configuration'));
  224. $this->drupalLogout();
  225. $this->drupalLogin($view_faq_user);
  226. $this->drupalGet('faq-page');
  227. $this->assertText(t('Frequently Asked Questions'), t('FAQ page is available for view faq page permissions.'));
  228. $this->assertText($this->faq1['title'], t('Created FAQ node 1 available on FAQ page.'));
  229. $this->assertText($this->faq1[$this->instance['field_name'] . '[' . $langcode .']'], t('Term for node 1 not available on FAQ page.'));
  230. $this->assertText($this->faq2['title'], t('Created FAQ node 2 available on FAQ page.'));
  231. $this->assertText($this->faq2[$this->instance['field_name'] . '[' . $langcode .']'], t('Term for node 2 not available on FAQ page.'));
  232. // TODO: Add update and Delete of FAQ nodes
  233. }
  234. }
  235. class CRAUDFaqTestCase extends FaqTestCase {
  236. /**
  237. * Implementation of getInfo().
  238. */
  239. public static function getInfo() {
  240. return array(
  241. 'name' => t('CRUD Question'),
  242. 'description' => t('Create, read, update and delete a question'),
  243. 'group' => t('Frequently Asked Questions'),
  244. );
  245. }
  246. /**
  247. * Test creating an FAQ and verify its status
  248. */
  249. public function testFaqCreate() {
  250. // Verify that logged in user has no access to the faq page
  251. $this->faqVerifyNoAccess('faq-page');
  252. // Log in user with create FAQ permissions
  253. $this->drupalLogin($this->faq_user);
  254. // Create a FAQ node.
  255. $edit = array();
  256. $edit['title'] = $this->randomName(8);
  257. $edit[$this->instance['field_name'] . '['. LANGUAGE_NONE .']'] = $this->randomName(8);
  258. $edit['detailed_question'] = $this->randomName(64);
  259. $edit['body['. LANGUAGE_NONE .'][0][value]'] = $this->randomString(264);
  260. $this->drupalPost('node/add/faq', $edit, t('Save'));
  261. $this->assertText(t('FAQ @title has been created.', array('@title' => $edit['title'])));
  262. // Check status for FAQ node - should be published
  263. $node = $this->drupalGetNodeByTitle($edit['title']);
  264. $this->assertTrue($node->status);
  265. $this->drupalLogout();
  266. // Verify that anonymous user has no access to the unanswered node display
  267. // $this->faqVerifyNoAccess('node/' . $node->nid);
  268. $this->drupalGet('node/' . $node->nid . '/edit'); // Open edit page with node
  269. // $this->assertResponse(200);
  270. // Create and log in user with view FAQ permissions
  271. $faq_view_user = $this->drupalCreateUser(array('view faq page'));
  272. $this->drupalLogin($faq_view_user);
  273. // Verify visibility on faq page
  274. $this->drupalGet('faq-page'); // Load faq page
  275. $this->assertText($edit['title']); // Node should be listed here
  276. $this->drupalGet('node/' . $node->nid); // It should also be possible to open the node directly
  277. // Update FAQ
  278. // Log in user with answer question. Must also have edit any faq content and view faq page permissions.
  279. $this->drupalLogin($this->faq_user);
  280. $edit2['title'] = 'title-' . $this->randomName(8);
  281. $edit2['body['. LANGUAGE_NONE .'][0][value]'] = 'body-' . $this->randomName(64);
  282. $this->drupalPost('node/' . $node->nid . '/edit', $edit2, t('Save'));
  283. $this->assertText(t('FAQ @title has been updated.', array('@title' => $edit2['title'])));
  284. $this->assertText($edit2['title'], 'Title has changed');
  285. $this->assertText($edit2['body['. LANGUAGE_NONE .'][0][value]'], 'Body has changed');
  286. // Delete FAQ
  287. // Try deleting faq by edit faq permission
  288. $this->drupalPost('node/' . $node->nid . '/edit', array(), t('Delete'));
  289. // Log in user with delete FAQ permissions
  290. $this->drupalLogin($this->admin_user);
  291. $this->drupalPost('node/' . $node->nid . '/edit', array(), t('Delete'));
  292. $this->assertText(t('Are you sure you want to delete @title?', array('@title' => $edit2['title'])));
  293. $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
  294. $this->assertText(t('FAQ @title has been deleted.', array('@title' => $edit2['title'])));
  295. }
  296. }