page_example.test

  1. examples
    1. 6 page_example/page_example.test
    2. 7 page_example/page_example.test
    3. 8 page_example/page_example.test

Test case for Testing the page example module.

This file contains the test cases to check if module is performing as expected.

Classes

NameDescription
PageExampleTestCase@file Test case for Testing the page example module.

File

page_example/page_example.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Test case for Testing the page example module.
  5. *
  6. * This file contains the test cases to check if module is performing as
  7. * expected.
  8. *
  9. */
  10. class PageExampleTestCase extends DrupalWebTestCase {
  11. protected $web_user;
  12. public static function getInfo() {
  13. return array(
  14. 'name' => 'Page example functionality',
  15. 'description' => 'Creates page and render the content based on the arguments passed in the URL.',
  16. 'group' => 'Examples',
  17. );
  18. }
  19. /**
  20. * Enable modules and create user with specific permissions.
  21. */
  22. function setUp() {
  23. parent::setUp('page_example');
  24. }
  25. /**
  26. * Generates a random string of ASCII numeric characters (values 48 to 57).
  27. *
  28. * @param $length
  29. * Length of random string to generate .
  30. * @return
  31. * Randomly generated string.
  32. */
  33. private static function randomNumber($length = 8) {
  34. $str = '';
  35. for ($i = 0; $i < $length; $i++) {
  36. $str .= chr(mt_rand(48, 57));
  37. }
  38. return $str;
  39. }
  40. /**
  41. * Verify that current user has no access to page.
  42. *
  43. * @param $url
  44. * URL to verify.
  45. */
  46. function pageExampleVerifyNoAccess($url) {
  47. // Test that page returns 403 Access Denied
  48. $this->drupalGet($url);
  49. $this->assertResponse(403);
  50. }
  51. /**
  52. * Login user, create an example node, and test blog functionality through the admin and user interfaces.
  53. */
  54. function testPageExampleBasic() {
  55. // Verify that anonymous user can't access the pages created by
  56. // page_example module
  57. $this->pageExampleVerifyNoAccess('examples/page_example/simple');
  58. $this->pageExampleVerifyNoAccess('examples/page_example/arguments/1/2');
  59. // Create a regular user and login.
  60. $this->web_user = $this->drupalCreateUser();
  61. $this->drupalLogin($this->web_user);
  62. // Verify that regular user can't access the pages created by
  63. // page_example module
  64. $this->pageExampleVerifyNoAccess('examples/page_example/simple');
  65. $this->pageExampleVerifyNoAccess('examples/page_example/arguments/1/2');
  66. // Create a user with permissions to access 'simple' page and login.
  67. $this->web_user = $this->drupalCreateUser(array('access simple page'));
  68. $this->drupalLogin($this->web_user);
  69. // Verify that user can access simple content
  70. $this->drupalGet('examples/page_example/simple');
  71. $this->assertResponse(200, t('simple content successfully accessed.'));
  72. $this->assertText(t('The quick brown fox jumps over the lazy dog.'), t('Simple content successfully verified.'));
  73. // Check if user can't access arguments page
  74. $this->pageExampleVerifyNoAccess('examples/page_example/arguments/1/2');
  75. // Create a user with permissions to access 'simple' page and login.
  76. $this->web_user = $this->drupalCreateUser(array('access arguments page'));
  77. $this->drupalLogin($this->web_user);
  78. // Verify that user can access simple content
  79. $first = $this->randomNumber(3);
  80. $second = $this->randomNumber(3);
  81. $this->drupalGet('examples/page_example/arguments/' . $first . '/' . $second);
  82. $this->assertResponse(200, t('arguments content successfully accessed.'));
  83. // Verify argument usage
  84. $this->assertRaw(t("First number was @number.", array('@number' => $first)), t('arguments first argument successfully verified.'));
  85. $this->assertRaw(t("Second number was @number.", array('@number' => $second)), t('arguments second argument successfully verified.'));
  86. $this->assertRaw(t('The total was @number.', array('@number' => $first + $second)), t('arguments content successfully verified.'));
  87. // Verify incomplete argument call to arguments content
  88. $this->drupalGet('examples/page_example/arguments/' . $first . '/');
  89. $this->assertText("provides two pages");
  90. // Verify invalid argument call to arguments content
  91. $this->drupalGet('examples/page_example/arguments/' . $first . '/' . $this->randomString());
  92. $this->assertResponse(403, t('Invalid argument for arguments content successfully verified'));
  93. // Verify invalid argument call to arguments content
  94. $this->drupalGet('examples/page_example/arguments/' . $this->randomString() . '/' . $second);
  95. $this->assertResponse(403, t('Invalid argument for arguments content successfully verified'));
  96. // Check if user can't access simple page
  97. $this->pageExampleVerifyNoAccess('examples/page_example/simple');
  98. }
  99. }
Login or register to post comments