PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/sally/tests/tests/Service/UserTest.php

https://bitbucket.org/mediastuttgart/sallycms-0.6
PHP | 144 lines | 91 code | 21 blank | 32 comment | 1 complexity | 3e0102d193d7107af7b8f89e7a3cc35e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. class sly_Service_UserTest extends sly_BaseTest {
  11. protected function getDataSetName() {
  12. return 'pristine-sally';
  13. }
  14. protected function getService() {
  15. static $service = null;
  16. if (!$service) $service = sly_Service_Factory::getUserService();
  17. return $service;
  18. }
  19. public function testCreate() {
  20. $service = $this->getService();
  21. $login = 'a'.uniqid();
  22. $user = $service->create(array(
  23. 'login' => $login,
  24. 'name' => 'Tom Tester',
  25. 'description' => 'Tom only exists during unit tests.',
  26. 'status' => true,
  27. 'createuser' => 'phpunit',
  28. 'updateuser' => 'phpunit',
  29. 'psw' => 'mumblefoo'
  30. ));
  31. $this->assertInstanceOf('sly_Model_User', $user);
  32. $this->assertEquals($login, $user->getLogin());
  33. $this->assertEquals('Tom Tester', $user->getName());
  34. $this->assertEquals('Tom only exists during unit tests.', $user->getDescription());
  35. $this->assertEquals(true, $user->getStatus());
  36. $this->assertNotEquals('mumblefoo', $user->getPassword());
  37. $this->assertFalse($user->isAdmin());
  38. $this->assertCount(2, $service->find());
  39. $this->assertInstanceOf('sly_Model_User', $service->findById($user->getId()));
  40. $this->assertInstanceOf('sly_Model_User', $service->findByLogin($login));
  41. }
  42. /**
  43. * @depends testCreate
  44. */
  45. public function testAdd() {
  46. $service = $this->getService();
  47. $login = 'a'.uniqid();
  48. $user = $service->add($login, 'mumblefoo', true, '');
  49. $this->assertInstanceOf('sly_Model_User', $user);
  50. $this->assertEquals($login, $user->getLogin());
  51. $this->assertEquals(true, $user->getStatus());
  52. $this->assertNotEquals('mumblefoo', $user->getPassword());
  53. $this->assertFalse($user->isAdmin());
  54. }
  55. /**
  56. * @depends testAdd
  57. */
  58. public function testEdit() {
  59. $service = $this->getService();
  60. $user = $service->add('a'.uniqid(), 'mumblefoo', true, '');
  61. $hash = $user->getPassword();
  62. $user->setName('F. Oooh');
  63. $user->setStatus(false);
  64. $user->setPassword('test');
  65. $service->save($user);
  66. $user = $service->findById($user->getId());
  67. $this->assertEquals('F. Oooh', $user->getName());
  68. $this->assertEquals(0, $user->getStatus());
  69. $this->assertNotEquals($hash, $user->getPassword());
  70. }
  71. /**
  72. * @depends testAdd
  73. */
  74. public function testKeepsPassword() {
  75. $service = $this->getService();
  76. $user = $service->add('a'.uniqid(), 'mumblefoo', true, '');
  77. $hash = $user->getPassword();
  78. $user->setName('F. Oooh');
  79. $service->save($user);
  80. $user = $service->findById($user->getId());
  81. $this->assertEquals($hash, $user->getPassword());
  82. }
  83. /**
  84. * @depends testCreate
  85. */
  86. public function testSalting() {
  87. $service = $this->getService();
  88. $userA = $service->create(array('login' => 'a'.uniqid(), 'psw' => 'mumblefoo', 'createdate' => time()));
  89. $userB = $service->create(array('login' => 'b'.uniqid(), 'psw' => 'mumblefoo', 'createdate' => time()-10));
  90. $this->assertNotEquals(sha1('mumblefoo'), $userA->getPassword()); // any salting at all?
  91. $this->assertNotEquals($userA->getPassword(), $userB->getPassword()); // user-specific salts?
  92. }
  93. /**
  94. * @expectedException sly_Exception
  95. */
  96. public function testMissingLogin() {
  97. $this->getService()->create(array('psw' => 'mumblefoo'));
  98. }
  99. /**
  100. * @expectedException sly_Exception
  101. */
  102. public function testMissingPassword() {
  103. $this->getService()->create(array('login' => 'a'.uniqid()));
  104. }
  105. /**
  106. * @depends testCreate
  107. */
  108. public function testZeroData() {
  109. $user = $this->getService()->create(array('login' => '0', 'psw' => '0'));
  110. $this->assertEquals('0', $user->getLogin());
  111. }
  112. /**
  113. * @depends testCreate
  114. */
  115. public function testDelete() {
  116. $service = $this->getService();
  117. $user = $service->create(array('login' => '0', 'psw' => '0'));
  118. $id = $user->getId();
  119. $service->delete(array('id' => $id));
  120. $this->assertNull($service->findById($id));
  121. $this->assertCount(1, $service->find());
  122. }
  123. }