PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/legacy/tests/StudentTest.php

https://github.com/zacharydanger/loose-leaf
PHP | 215 lines | 155 code | 31 blank | 29 comment | 1 complexity | 09bddc2229a0ece897ea530d2a7bab44 MD5 | raw file
  1. <?php
  2. require_once 'global.php';
  3. require_once 'PHPUnit/Framework.php';
  4. /**
  5. * Test class for Student.
  6. * Generated by PHPUnit on 2009-02-12 at 12:00:17.
  7. */
  8. class StudentTest extends PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var Student
  12. * @access protected
  13. */
  14. protected $object;
  15. public function testSetEmailAddress() {
  16. $customer_1 = new Student();
  17. $email = sha1(time() . rand(10,99)) . '@localhost.com';
  18. $this->assertEquals($email, $customer_1->setEmail($email));
  19. $customer_1->write();
  20. $this->assertTrue($customer_1->exists());
  21. $this->assertEquals($email, $customer_1->setEmail($email));
  22. $customer_2 = new Student();
  23. $this->assertNotEquals($email, $customer_2->setEmail($email));
  24. $this->assertEquals("", trim($customer_2->getEmail()));
  25. }
  26. public function customerProvider() {
  27. $sql = "SELECT student_id
  28. FROM students";
  29. $query = db_query_random($sql);
  30. $array = array();
  31. foreach($query as $rec) {
  32. $array[] = array($rec['customer_id']);
  33. }
  34. return $array;
  35. }
  36. /**
  37. * Sets up the fixture, for example, opens a network connection.
  38. * This method is called before a test is executed.
  39. *
  40. * @access protected
  41. */
  42. protected function setUp() {
  43. $this->object = new Student;
  44. $C = new Student();
  45. $C->email = md5(uniqid(rand(), true)) . '@localhost.foo';
  46. $C->name = 'foobar foo';
  47. $C->write();
  48. $this->fixture['customer'] = $C;
  49. }
  50. /**
  51. * Tears down the fixture, for example, closes a network connection.
  52. * This method is called after a test is executed.
  53. *
  54. * @access protected
  55. */
  56. protected function tearDown() {
  57. $this->fixture['customer']->delete();
  58. }
  59. /**
  60. * @dataProvider customerProvider
  61. */
  62. public function testLoginFail($customer_id) {
  63. $C = new Student($customer_id);
  64. $login_customer = new Student();
  65. $email = $C->getEmail();
  66. $pass = sha1($C->getPassword() . time() . rand(0, 100));
  67. $this->assertFalse($login_customer->login($email, $pass));
  68. }
  69. public function testLoginSuccess() {
  70. $C = new Student();
  71. $email = sha1(microtime() . rand(0,99)) . '@localhost.foo';
  72. $pass = sha1(microtime() . rand(0,99));
  73. $C->setEmail($email);
  74. $this->assertTrue($C->setPassword($pass));
  75. $C->newToken();
  76. $C->write();
  77. $this->assertTrue($C->exists());
  78. $new_customer = new Student();
  79. $this->assertTrue($new_customer->login($email, $pass), print_r($new_customer,true) . print_r($C, true));
  80. $C->delete();
  81. $this->assertFalse($C->exists());
  82. }
  83. public function testLogout() {
  84. $C = new Student();
  85. $email = sha1(microtime() . rand(0,99)) . '@localhost.foo';
  86. $pass = sha1(microtime() . rand(0,99));
  87. $C->setEmail($email);
  88. $this->assertTrue($C->setPassword($pass));
  89. $C->newToken();
  90. $C->write();
  91. $this->assertTrue($C->exists());
  92. $new_customer = new Student();
  93. $this->assertTrue($new_customer->login($email, $pass)); //, print_r($new_customer,true));
  94. $token = $new_customer->getToken();
  95. $this->assertGreaterThan(0, strlen($token));
  96. $this->assertNotNull($token);
  97. $new_customer->logout();
  98. $LOOKUP = User_Session::tokenFactory($token);
  99. $this->assertTrue(is_a($LOOKUP, 'Student'));
  100. $this->assertFalse($LOOKUP->exists());
  101. }
  102. /**
  103. * @dataProvider customerProvider
  104. */
  105. public function testSetPassword($customer_id) {
  106. $C = new Student($customer_id);
  107. $this->assertFalse($C->setPassword("asdf"), "Password set without pre-existing password.");
  108. $this->assertFalse($C->setPassword("asdf", sha1("asdf")));
  109. unset($C);
  110. $C = new Student();
  111. $bad_password = "";
  112. while(strlen($bad_password) < (MIN_PASSWORD_LENGTH - 1)) {
  113. $bad_password .= rand(0,9);
  114. }
  115. $this->assertFalse($C->setPassword($bad_password), "Password set with bad password. '$bad_password' " . MIN_PASSWORD_LENGTH);
  116. unset($C);
  117. $C = new Student();
  118. $good_password = $bad_password . rand(0,9);
  119. $this->assertTrue($C->setPassword($good_password), "New password not set with good password. '$good_password' " . MIN_PASSWORD_LENGTH);
  120. $C->setEmail(sha1(time()) . '@localhost.foo');
  121. $C->newToken();
  122. $C->write();
  123. $this->assertTrue($C->exists());
  124. $this->assertTrue($C->setPassword(sha1(time()), $good_password));
  125. $C->delete();
  126. $this->assertFalse($C->exists());
  127. }
  128. public function testResetPassword() {
  129. $C = new Student();
  130. $C->name = 'Zach Campbell';
  131. $C->email = sha1(microtime() * rand(1,9)) . '@localhost.com';
  132. $original_password = sha1(rand(1,100) . time());
  133. $this->assertTrue($C->setPassword($original_password), print_r($C,true));
  134. $C->newToken();
  135. $C->write();
  136. $this->assertEquals(passwordify($original_password, $C->salt), $C->password, print_r($C, true));
  137. recover_customer_password($C);
  138. $sql = "SELECT token
  139. FROM `user_password_tokens`
  140. WHERE user_id = '" . intval($C->ID) . "'
  141. AND user_type = '" . db_input(get_class($C)) . "'
  142. AND expiration > now()";
  143. $query = db_query($sql);
  144. $this->assertGreaterThan(0, $query->num_rows);
  145. $old_password = passwordify($original_password);
  146. while($query->num_rows > 0 && $t = $query->fetch_assoc()) {
  147. $new_password = sha1(rand(1,100) . microtime());
  148. $this->assertTrue($C->resetPassword($new_password, $t['token']));
  149. $this->assertEquals(passwordify($new_password, $C->salt), $C->password);
  150. $this->assertFalse($C->resetPassword(null, $t['token']));
  151. $old_password = passwordify($new_password);
  152. }
  153. }
  154. public function testResetBadPassword() {
  155. $C = new Student();
  156. $C->name = 'Zach Campbell';
  157. $email = sha1(microtime() * rand(1,9)) . '@localhost.foo';
  158. $this->assertEquals($email, $C->setEmail($email));
  159. $original_password = sha1(rand(1,100) . microtime());
  160. $this->assertTrue($C->setPassword($original_password), print_r($C,true));
  161. $C->write();
  162. $this->assertEquals(passwordify($original_password, $C->salt), $C->password, print_r($C, true));
  163. $this->assertFalse($C->resetPassword(sha1(microtime), 'bad tokenz0rz'));
  164. }
  165. public function testSetToken() {
  166. $C = new Student();
  167. $token = sha1(time() . rand(0,99));
  168. $C->setToken($token);
  169. $this->assertNotEquals($token, $C->getSessionToken());
  170. }
  171. /**
  172. * @dataProvider customerProvider
  173. */
  174. public function testGetAddressBookDataDump($customer_id) {
  175. $C = new Student($customer_id);
  176. $address_book_dump = $C->getAddressBook(true);
  177. $this->assertType('array', $address_book_dump);
  178. }
  179. public function testUserType() {
  180. $C = new Student();
  181. $this->assertEquals(User::TYPE_STUDENT, $C->getUserType());
  182. }
  183. }
  184. ?>