shibboleth_test.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Shibboleth;
  3. /**
  4. * Various Unit Tests. Note that those tests create users in the database but
  5. * don't delete them.
  6. *
  7. * @license see /license.txt
  8. * @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
  9. */
  10. class ShibbolethTest
  11. {
  12. static function is_enabled()
  13. {
  14. return api_get_setting('server_type') == 'test';
  15. }
  16. /**
  17. * @return ShibbolethTestHelper
  18. */
  19. static function helper()
  20. {
  21. return ShibbolethTestHelper::instance();
  22. }
  23. static function init()
  24. {
  25. if (!self::is_enabled())
  26. {
  27. die;
  28. }
  29. }
  30. static function test_new_teacher()
  31. {
  32. self::init();
  33. self::helper()->setup_new_teacher();
  34. $shib_user = Shibboleth::store()->get_user();
  35. self::assert(!User::store()->shibboleth_id_exists($shib_user->unique_id));
  36. Shibboleth::save($shib_user);
  37. $user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
  38. self::assert($user->email == $shib_user->email);
  39. self::assert($user->firstname == $shib_user->firstname);
  40. self::assert($user->lastname == $shib_user->lastname);
  41. self::assert($user->persistent_id == $shib_user->persistent_id);
  42. self::assert($user->status == Shibboleth::TEACHER_STATUS);
  43. self::assert(!empty($user->password));
  44. self::assert(!empty($user->username));
  45. }
  46. static function test_new_student()
  47. {
  48. self::init();
  49. self::helper()->setup_new_student();
  50. $shib_user = Shibboleth::store()->get_user();
  51. self::assert(!User::store()->shibboleth_id_exists($shib_user->unique_id));
  52. Shibboleth::save($shib_user);
  53. $user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
  54. self::assert($user->email == $shib_user->email);
  55. self::assert($user->firstname == $shib_user->firstname);
  56. self::assert($user->lastname == $shib_user->lastname);
  57. self::assert($user->persistent_id == $shib_user->persistent_id);
  58. self::assert($user->status == Shibboleth::STUDENT_STATUS);
  59. self::assert(!empty($user->password));
  60. self::assert(!empty($user->username));
  61. }
  62. static function test_new_staff()
  63. {
  64. self::init();
  65. self::helper()->setup_new_staff();
  66. $shib_user = Shibboleth::store()->get_user();
  67. self::assert(!User::store()->shibboleth_id_exists($shib_user->unique_id));
  68. Shibboleth::save($shib_user);
  69. $user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
  70. self::assert($user->email == $shib_user->email);
  71. self::assert($user->firstname == $shib_user->firstname);
  72. self::assert($user->lastname == $shib_user->lastname);
  73. self::assert($user->persistent_id == $shib_user->persistent_id);
  74. self::assert($user->status == Shibboleth::STUDENT_STATUS);
  75. self::assert(!empty($user->password));
  76. self::assert(!empty($user->username));
  77. }
  78. static function test_new_infer_status_request()
  79. {
  80. self::init();
  81. self::helper()->setup_new_staff();
  82. $shib_user = Shibboleth::store()->get_user();
  83. Shibboleth::save($shib_user);
  84. self::assert($shib_user->status_request);
  85. self::helper()->setup_new_teacher();
  86. $shib_user = Shibboleth::store()->get_user();
  87. Shibboleth::save($shib_user);
  88. self::assert(!$shib_user->status_request);
  89. self::helper()->setup_new_student();
  90. $shib_user = Shibboleth::store()->get_user();
  91. Shibboleth::save($shib_user);
  92. self::assert(!$shib_user->status_request);
  93. }
  94. static function test_update_teacher()
  95. {
  96. self::init();
  97. $fields = Shibboleth::config()->update_fields;
  98. self::assert($fields['email']);
  99. self::assert($fields['persistent_id']);
  100. self::assert($fields['firstname']);
  101. self::assert($fields['lastname']);
  102. self::assert(!$fields['status']);
  103. self::helper()->setup_teacher();
  104. $shib_user = Shibboleth::store()->get_user();
  105. Shibboleth::save($shib_user);
  106. $new_shib_user = clone($shib_user);
  107. $new_shib_user->firstname = 'frs';
  108. $new_shib_user->lastname = 'ls';
  109. $new_shib_user->email = 'em';
  110. $new_shib_user->status = 10;
  111. $new_shib_user->persistent_id = 'per';
  112. Shibboleth::save($new_shib_user);
  113. $user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
  114. self::assert($user->email == $new_shib_user->email);
  115. self::assert($value = ($user->shibb_persistent_id == $new_shib_user->persistent_id));
  116. self::assert($user->firstname == $new_shib_user->firstname);
  117. self::assert($user->lastname == $new_shib_user->lastname);
  118. self::assert($user->status == $shib_user->status);
  119. self::assert(!empty($user->password));
  120. self::assert(!empty($user->username));
  121. }
  122. static function test_new_student_multiple_givenname()
  123. {
  124. self::init();
  125. self::helper()->setup_new_student_multiple_givenname();
  126. $shib_user = Shibboleth::store()->get_user();
  127. self::assert(!User::store()->shibboleth_id_exists($shib_user->unique_id));
  128. Shibboleth::save($shib_user);
  129. $user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
  130. self::assert($user->email == $shib_user->email);
  131. self::assert($user->firstname == 'John');
  132. self::assert($user->lastname == $shib_user->lastname);
  133. self::assert($user->persistent_id == $shib_user->persistent_id);
  134. self::assert($user->status == Shibboleth::STUDENT_STATUS);
  135. self::assert(!empty($user->password));
  136. self::assert(!empty($user->username));
  137. }
  138. static function test_new_no_affiliation_default()
  139. {
  140. self::init();
  141. self::helper()->setup_new_no_affiliation();
  142. $shib_user = Shibboleth::store()->get_user();
  143. self::assert($config = Shibboleth::config()->default_status == Shibboleth::STUDENT_STATUS);
  144. self::assert(!User::store()->shibboleth_id_exists($shib_user->unique_id));
  145. self::assert($shib_user->affiliation == '');
  146. Shibboleth::save($shib_user);
  147. $user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
  148. self::assert($user->email == $shib_user->email);
  149. self::assert($user->firstname == 'John');
  150. self::assert($user->lastname == $shib_user->lastname);
  151. self::assert($user->persistent_id == $shib_user->persistent_id);
  152. self::assert($user->status == Shibboleth::STUDENT_STATUS);
  153. self::assert(!empty($user->password));
  154. self::assert(!empty($user->username));
  155. }
  156. static function assert($assertion, $message = '')
  157. {
  158. if (!$assertion)
  159. {
  160. $message = "Assert failed $message <br/>";
  161. echo $message;
  162. // Dump variable for debug
  163. var_dump(debug_backtrace());
  164. die;
  165. }
  166. else
  167. {
  168. $message = "Assert successful $message <br/>";
  169. echo $message;
  170. }
  171. }
  172. }