page_example.test 4.4 KB

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