PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/package/app/app/tests/roles_and_permissions/UserRoleServiceUnitTest.php

https://bitbucket.org/pandaos/kaltura
PHP | 616 lines | 461 code | 85 blank | 70 comment | 30 complexity | 47292c4399d79f3237fb504fa3971c4e MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, BSD-3-Clause, LGPL-2.1, GPL-2.0, LGPL-3.0, JSON, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. require_once 'PHPUnit\Framework\TestCase.php';
  3. require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'bootstrap.php');
  4. require_once(KALTURA_CLIENT_PATH);
  5. /**
  6. * test case.
  7. */
  8. class UserRoleServiceUnitTest extends PHPUnit_Framework_TestCase {
  9. const TEST_PARTNER_ID = null;
  10. const TEST_ADMIN_SECRET = null;
  11. const TEST_USER_SECRET = null;
  12. private $addedRoleIds = array();
  13. /**
  14. * @var KalturaClient
  15. */
  16. private $client = null;
  17. /**
  18. * Prepares the environment before running a test.
  19. */
  20. protected function setUp() {
  21. if ( !self::TEST_PARTNER_ID || !self::TEST_ADMIN_SECRET || !self::TEST_USER_SECRET )
  22. {
  23. die('Test partners were not defined - quitting test!');
  24. }
  25. parent::setUp ();
  26. $this->client = $this->getClient(self::TEST_PARTNER_ID);
  27. $this->dummyPartner = PartnerPeer::retrieveByPK(self::TEST_PARTNER_ID);
  28. $this->assertEquals(self::TEST_PARTNER_ID, $this->dummyPartner->getId());
  29. $this->addedRoleIds = array();
  30. UserRolePeer::clearInstancePool();
  31. PermissionPeer::clearInstancePool();
  32. PermissionItemPeer::clearInstancePool();
  33. kuserPeer::clearInstancePool();
  34. PartnerPeer::clearInstancePool();
  35. }
  36. /**
  37. * Cleans up the environment after running a test.
  38. */
  39. protected function tearDown() {
  40. UserRolePeer::clearInstancePool();
  41. PermissionPeer::clearInstancePool();
  42. PermissionItemPeer::clearInstancePool();
  43. kuserPeer::clearInstancePool();
  44. PartnerPeer::clearInstancePool();
  45. $this->client = null;
  46. UserRolePeer::setUseCriteriaFilter(false);
  47. foreach ($this->addedRoleIds as $id) {
  48. try
  49. {
  50. $obj = UserRolePeer::retrieveByPK($id);
  51. if ($obj) {
  52. $obj->delete();
  53. }
  54. }
  55. catch (PropelException $e) {}
  56. }
  57. UserRolePeer::setUseCriteriaFilter(true);
  58. $this->addedRoleIds = array();
  59. parent::tearDown ();
  60. }
  61. /**
  62. * @param int $partnerId
  63. * @return KalturaClient
  64. */
  65. private function getClient($partnerId)
  66. {
  67. if ($partnerId) {
  68. $config = new KalturaConfiguration($partnerId);
  69. }
  70. else {
  71. $config = new KalturaConfiguration();
  72. }
  73. $config->serviceUrl = kConf::get('apphome_url');
  74. $client = new KalturaClient($config);
  75. return $client;
  76. }
  77. /**
  78. * Starts a new session
  79. * @param KalturaSessionType $type
  80. * @param string $userId
  81. */
  82. private function startSession($type, $userId)
  83. {
  84. $secret = ($type == KalturaSessionType::ADMIN) ? self::TEST_ADMIN_SECRET : self::TEST_USER_SECRET;
  85. $ks = $this->client->session->start($secret, $userId, $type, self::TEST_PARTNER_ID);
  86. $this->assertNotNull($ks);
  87. if (!$ks) {
  88. return false;
  89. }
  90. $this->client->setKs($ks);
  91. return true;
  92. }
  93. private function checkException($exceptionThrown, $code = null, $message = null)
  94. {
  95. if (!$exceptionThrown) {
  96. $this->fail('No exception was thrown');
  97. }
  98. if ($code && $exceptionThrown->getCode() != $code) {
  99. $this->fail('Exception thrown with code ['.$exceptionThrown->getCode().'] instead of ['.$code.']');
  100. }
  101. if ($message && $exceptionThrown->getMessage() != $message) {
  102. $this->fail('Exception thrown with message ['.$exceptionThrown->getMessage().'] instead of ['.$message.']');
  103. }
  104. }
  105. /**
  106. * @return Partner
  107. */
  108. private function getDbPartner()
  109. {
  110. return PartnerPeer::retrieveByPK(self::TEST_PARTNER_ID);
  111. }
  112. private function addRoleWrap(KalturaUserRole $role)
  113. {
  114. $addedRole = $this->client->userRole->add($role);
  115. $this->addedRoleIds[] = $addedRole->id;
  116. return $addedRole;
  117. }
  118. public function testFailuresUserSession()
  119. {
  120. // failure to make all requests with a user KS instead of an admin KS
  121. $this->startSession(KalturaSessionType::USER, null); // start a user session
  122. // add action
  123. $exceptionThrown = false;
  124. try { $this->addRoleWrap(new KalturaUserRole()); }
  125. catch (Exception $e) { $exceptionThrown = $e; }
  126. $this->checkException($exceptionThrown, 'SERVICE_FORBIDDEN');
  127. // clone action
  128. $exceptionThrown = false;
  129. try { $this->client->userRole->cloneAction(rand(0, 10)); }
  130. catch (Exception $e) { $exceptionThrown = $e; }
  131. $this->checkException($exceptionThrown, 'SERVICE_FORBIDDEN');
  132. // delete action
  133. $exceptionThrown = false;
  134. try { $this->client->userRole->delete(rand(0, 10)); }
  135. catch (Exception $e) { $exceptionThrown = $e; }
  136. $this->checkException($exceptionThrown, 'SERVICE_FORBIDDEN');
  137. // get action
  138. $exceptionThrown = false;
  139. try { $this->client->userRole->get(rand(0, 10)); }
  140. catch (Exception $e) { $exceptionThrown = $e; }
  141. $this->checkException($exceptionThrown, 'SERVICE_FORBIDDEN');
  142. // list action
  143. $exceptionThrown = false;
  144. try { $this->client->userRole->listAction(); }
  145. catch (Exception $e) { $exceptionThrown = $e; }
  146. $this->checkException($exceptionThrown, 'SERVICE_FORBIDDEN');
  147. // update action
  148. $exceptionThrown = false;
  149. try { $this->client->userRole->update(rand(0, 10), new KalturaUserRole()); }
  150. catch (Exception $e) { $exceptionThrown = $e; }
  151. $this->checkException($exceptionThrown, 'SERVICE_FORBIDDEN');
  152. }
  153. public function testAddAction()
  154. {
  155. $this->startSession(KalturaSessionType::ADMIN, $this->getDbPartner()->getAdminUserId());
  156. // add role
  157. $newRole = new KalturaUserRole();
  158. $newRole->name = 'New test role'.uniqid();
  159. $newRole->description = 'Test description'.uniqid();
  160. $newRole->status = KalturaUserRoleStatus::ACTIVE;
  161. $newRole->permissionNames = KalturaPermissionName::ACCESS_CONTROL_BASE.','.KalturaPermissionName::INTEGRATION_BASE;
  162. $addedRole = $this->addRoleWrap($newRole);
  163. // verify the returned role object's parameters
  164. $this->assertType('KalturaUserRole', $addedRole);
  165. $this->assertEquals($newRole->name, $addedRole->name);
  166. $this->assertEquals($newRole->description, $addedRole->description);
  167. $this->assertEquals($newRole->status, $addedRole->status);
  168. $this->assertEquals($newRole->permissionNames, $addedRole->permissionNames);
  169. $this->assertEquals(self::TEST_PARTNER_ID, $addedRole->partnerId);
  170. $this->assertNotNull($addedRole->id);
  171. $this->assertNotNull($addedRole->createdAt);
  172. $this->assertNotNull($addedRole->updatedAt);
  173. // try to get the role and verify returned parameters
  174. $getRole = $this->client->userRole->get($addedRole->id);
  175. $this->assertType('KalturaUserRole', $getRole);
  176. $this->assertEquals($newRole->name, $getRole->name);
  177. $this->assertEquals($newRole->description, $getRole->description);
  178. $this->assertEquals($newRole->status, $getRole->status);
  179. $this->assertEquals($newRole->permissionNames, $getRole->permissionNames);
  180. $this->assertEquals(self::TEST_PARTNER_ID, $getRole->partnerId);
  181. $this->assertEquals($addedRole->id, $getRole->id);
  182. $this->assertEquals($addedRole->createdAt, $getRole->createdAt);
  183. $this->assertEquals($addedRole->updatedAt, $getRole->updatedAt);
  184. // get the role in a list
  185. $roleList = $this->client->userRole->listAction();
  186. $roleList = $roleList->objects;
  187. $found = false;
  188. foreach ($roleList as $role) {
  189. if ($role->id === $getRole->id && $role->name === $getRole->name) {
  190. $found = true;
  191. break;
  192. }
  193. }
  194. if (!$found) {
  195. $this->fail('New added role with id ['.$getRole->id.'] was not returned from userRole->list action');
  196. }
  197. // add a role with no status and verify that it is set to ACTIVE
  198. $newRole = new KalturaUserRole();
  199. $newRole->name = 'Test role with no status'.uniqid();
  200. $addedRole = $this->addRoleWrap($newRole);
  201. $this->assertEquals(KalturaUserRoleStatus::ACTIVE, $addedRole->status);
  202. }
  203. public function testAddActionFailures()
  204. {
  205. $this->startSession(KalturaSessionType::ADMIN, $this->getDbPartner()->getAdminUserId());
  206. $failedRoleNames = array();
  207. // failure to add a role with no name
  208. $newRole = new KalturaUserRole();
  209. $exceptionThrown = false;
  210. try { $addedRole = $this->addRoleWrap($newRole); }
  211. catch (Exception $e) { $exceptionThrown = $e; }
  212. $this->checkException($exceptionThrown, 'PROPERTY_VALIDATION_CANNOT_BE_NULL');
  213. $failedRoleNames[] = $newRole->name;
  214. // failure to add a role with an invalid permission
  215. $newRole = new KalturaUserRole();
  216. $newRole->name = 'Stam 1'.uniqid();
  217. $newRole->permissionNames = uniqid();
  218. $exceptionThrown = false;
  219. try { $addedRole = $this->addRoleWrap($newRole); }
  220. catch (Exception $e) { $exceptionThrown = $e; }
  221. $this->checkException($exceptionThrown, 'PERMISSION_NOT_FOUND');
  222. $failedRoleNames[] = $newRole->name;
  223. // failure to add a role with a permission which the partner does not have
  224. $newRole = new KalturaUserRole();
  225. $newRole->name = 'Stam 2'.uniqid();
  226. $newRole->permissionNames = PermissionName::BATCH_BASE;
  227. $exceptionThrown = false;
  228. try { $addedRole = $this->addRoleWrap($newRole); }
  229. catch (Exception $e) { $exceptionThrown = $e; }
  230. $this->checkException($exceptionThrown, 'PERMISSION_NOT_FOUND');
  231. $failedRoleNames[] = $newRole->name;
  232. // failure to choose role's ID
  233. $newRole = new KalturaUserRole();
  234. $newRole->name = 'Stam 3'.uniqid();
  235. $newRole->id = rand(1,100);
  236. $exceptionThrown = false;
  237. try { $addedRole = $this->addRoleWrap($newRole); }
  238. catch (Exception $e) { $exceptionThrown = $e; }
  239. $this->checkException($exceptionThrown, 'PROPERTY_VALIDATION_NOT_UPDATABLE');
  240. $failedRoleNames[] = $newRole->name;
  241. // failure to choose role's partner ID
  242. $newRole = new KalturaUserRole();
  243. $newRole->name = 'Stam 4'.uniqid();
  244. $newRole->partnerId = rand(100, 300);
  245. $exceptionThrown = false;
  246. try { $addedRole = $this->addRoleWrap($newRole); }
  247. catch (Exception $e) { $exceptionThrown = $e; }
  248. $this->checkException($exceptionThrown, 'PROPERTY_VALIDATION_NOT_UPDATABLE');
  249. $failedRoleNames[] = $newRole->name;
  250. // verify that none of the failed roles is returned in the roles list
  251. $roleList = $this->client->userRole->listAction();
  252. $roleList = $roleList->objects;
  253. foreach ($roleList as $role) {
  254. if (in_array($role->name, $failedRoleNames)) {
  255. $this->fail('Failed role with name ['.$role->name.'] was mistakenly added and returned in list with ID ['.$role->id.']');
  256. }
  257. }
  258. }
  259. public function testCloneAction()
  260. {
  261. $this->startSession(KalturaSessionType::ADMIN, $this->getDbPartner()->getAdminUserId());
  262. // failure to clone an invalid role
  263. $roleId = rand(-100, -1);
  264. $exceptionThrown = false;
  265. try { $this->client->userRole->cloneAction($roleId); }
  266. catch (Exception $e) { $exceptionThrown = $e; }
  267. $this->checkException($exceptionThrown, 'INVALID_OBJECT_ID');
  268. // failure to clone another partner's role
  269. $c = new Criteria();
  270. $c->addAnd(UserRolePeer::PARTNER_ID, array(Partner::ADMIN_CONSOLE_PARTNER_ID, Partner::BATCH_PARTNER_ID), Criteria::IN);
  271. $systemPartnersRoles = UserRolePeer::doSelect($c);
  272. foreach ($systemPartnersRoles as $systemPartnerRole)
  273. {
  274. $exceptionThrown = false;
  275. try { $this->client->userRole->cloneAction($systemPartnerRole->getId()); }
  276. catch (Exception $e) { $exceptionThrown = $e; }
  277. $this->checkException($exceptionThrown, 'INVALID_OBJECT_ID');
  278. }
  279. // failure to clone a deleted role
  280. $newRole = new KalturaUserRole();
  281. $newRole->name = 'Deleted role'.uniqid();
  282. $addedRole = $this->addRoleWrap($newRole);
  283. $this->client->userRole->delete($addedRole->id);
  284. $exceptionThrown = false;
  285. try { $this->client->userRole->cloneAction($addedRole->id); }
  286. catch (Exception $e) { $exceptionThrown = $e; }
  287. $this->checkException($exceptionThrown, 'INVALID_OBJECT_ID');
  288. // clone a valid role and validate all fields except for ID
  289. $newRole = new KalturaUserRole();
  290. $newRole->name = 'New test role to clone'.uniqid();
  291. $newRole->description = 'Test description'.uniqid();
  292. $newRole->permissionNames = KalturaPermissionName::ADMIN_ROLE_DELETE.','.KalturaPermissionName::CONTENT_MANAGE_RECONVERT;
  293. $addedRole = $this->addRoleWrap($newRole); // add a new role
  294. $clonedRole = $this->client->userRole->cloneAction($addedRole->id); // clone role
  295. $this->addedRoleIds[] = $clonedRole->id;
  296. $this->assertType('KalturaUserRole', $clonedRole);
  297. $this->assertGreaterThan($addedRole->id, $clonedRole->id);
  298. $this->assertEquals($newRole->name, $addedRole->name);
  299. $this->assertEquals($newRole->description, $addedRole->description);
  300. $this->assertEquals(KalturaUserRoleStatus::ACTIVE, $clonedRole->status);
  301. $this->assertEquals($newRole->permissionNames, $clonedRole->permissionNames);
  302. $this->assertEquals(self::TEST_PARTNER_ID, $clonedRole->partnerId);
  303. $this->assertNotNull($clonedRole->createdAt);
  304. $this->assertNotNull($clonedRole->updatedAt);
  305. }
  306. public function testDeleteAction()
  307. {
  308. $this->startSession(KalturaSessionType::ADMIN, $this->getDbPartner()->getAdminUserId());
  309. // failure to delete partner 0 role
  310. $c = new Criteria();
  311. $c->addAnd(UserRolePeer::PARTNER_ID, PartnerPeer::GLOBAL_PARTNER, Criteria::EQUAL);
  312. $partner0Roles = UserRolePeer::doSelect($c);
  313. foreach ($partner0Roles as $role)
  314. {
  315. $exceptionThrown = false;
  316. try { $this->client->userRole->delete($role->getId()); }
  317. catch (Exception $e) { $exceptionThrown = $e; }
  318. $this->checkException($exceptionThrown, 'INVALID_OBJECT_ID');
  319. }
  320. // failure to delete another partner's role
  321. $c = new Criteria();
  322. $c->addAnd(UserRolePeer::PARTNER_ID, self::TEST_PARTNER_ID, Criteria::NOT_EQUAL);
  323. $otherPartnerRoles = UserRolePeer::doSelect($c);
  324. for ($i=1; $i<4; $i++)
  325. {
  326. $randId = rand(0, count($otherPartnerRoles)-1);
  327. $exceptionThrown = false;
  328. try { $this->client->userRole->delete($otherPartnerRoles[$randId]->getId()); }
  329. catch (Exception $e) { $exceptionThrown = $e; }
  330. $this->checkException($exceptionThrown, 'INVALID_OBJECT_ID');
  331. }
  332. // success deleting current partner's role
  333. $newRole = new KalturaUserRole();
  334. $newRole->name = 'Deleted role'.uniqid();
  335. $addedRole = $this->addRoleWrap($newRole);
  336. $getRole = $this->client->userRole->get($addedRole->id);
  337. $this->assertEquals($newRole->name, $getRole->name);
  338. $deletedRole = $this->client->userRole->delete($addedRole->id);
  339. $this->assertEquals($newRole->name, $deletedRole->name);
  340. $this->assertEquals(KalturaUserRoleStatus::DELETED, $deletedRole->status);
  341. // verify that deleted role is not returned in get or list
  342. $exceptionThrown = false;
  343. try { $this->client->userRole->get($addedRole->id); }
  344. catch (Exception $e) { $exceptionThrown = $e; }
  345. $this->checkException($exceptionThrown, 'INVALID_OBJECT_ID');
  346. $roleList = $this->client->userRole->listAction();
  347. $roleList = $roleList->objects;
  348. foreach ($roleList as $role) {
  349. if ($role->name == $newRole->name) {
  350. $this->fail('Deleted role with name ['.$role->name.'] was mistakenly returned in list with ID ['.$role->id.']');
  351. }
  352. }
  353. }
  354. public function testGetAction()
  355. {
  356. $this->startSession(KalturaSessionType::ADMIN, $this->getDbPartner()->getAdminUserId());
  357. // get a partner 0 role and compare to DB record
  358. $c = new Criteria();
  359. $c->addAnd(UserRolePeer::PARTNER_ID, PartnerPeer::GLOBAL_PARTNER, Criteria::EQUAL);
  360. $partner0Roles = UserRolePeer::doSelect($c);
  361. for ($i=1; $i<4; $i++)
  362. {
  363. $randId = rand(0, count($partner0Roles)-1);
  364. $getRole = $this->client->userRole->get($partner0Roles[$randId]->getId());
  365. $this->assertType('KalturaUserRole', $getRole);
  366. $this->assertEquals(PartnerPeer::GLOBAL_PARTNER, $getRole->partnerId);
  367. $this->assertEquals($partner0Roles[$randId]->getId(), $getRole->id);
  368. $this->assertEquals($partner0Roles[$randId]->getName(), $getRole->name);
  369. $this->assertEquals($partner0Roles[$randId]->getDescription(), $getRole->description);
  370. $this->assertEquals($partner0Roles[$randId]->getPartnerId(), $getRole->partnerId);
  371. $this->assertEquals($partner0Roles[$randId]->getPermissionNames(), $getRole->permissionNames);
  372. $this->assertNotNull($getRole->createdAt);
  373. $this->assertNotNull($getRole->updatedAt);
  374. }
  375. // get current partner's role and compare to DB record
  376. $c = new Criteria();
  377. $c->addAnd(UserRolePeer::PARTNER_ID, self::TEST_PARTNER_ID, Criteria::EQUAL);
  378. $partnerRoles = UserRolePeer::doSelect($c);
  379. for ($i=1; $i<4; $i++)
  380. {
  381. $randId = rand(0, count($partner0Roles)-1);
  382. $getRole = $this->client->userRole->get($partnerRoles[$randId]->getId());
  383. $this->assertType('KalturaUserRole', $getRole);
  384. $this->assertEquals(self::TEST_PARTNER_ID, $getRole->partnerId);
  385. $this->assertEquals($partnerRoles[$randId]->getId(), $getRole->id);
  386. $this->assertEquals($partnerRoles[$randId]->getName(), $getRole->name);
  387. $this->assertEquals($partnerRoles[$randId]->getDescription(), $getRole->description);
  388. $this->assertEquals($partnerRoles[$randId]->getPartnerId(), $getRole->partnerId);
  389. $this->assertEquals($partnerRoles[$randId]->getPermissionNames(), $getRole->permissionNames);
  390. $this->assertNotNull($getRole->createdAt);
  391. }
  392. // failure to get another partner's role (not partner 0)
  393. $c = new Criteria();
  394. $c->addAnd(UserRolePeer::PARTNER_ID, array(self::TEST_PARTNER_ID, PartnerPeer::GLOBAL_PARTNER), Criteria::NOT_IN);
  395. $otherPartnerRoles = UserRolePeer::doSelect($c);
  396. for ($i=1; $i<4; $i++)
  397. {
  398. $randId = rand(0, count($partner0Roles)-1);
  399. $exceptionThrown = false;
  400. try { $this->client->userRole->get($otherPartnerRoles[$randId]->getId()); }
  401. catch (Exception $e) { $exceptionThrown = $e; }
  402. $this->checkException($exceptionThrown, 'INVALID_OBJECT_ID');
  403. }
  404. // add role with permission names = * and verify that all relevant permissions are returned
  405. $newRole = new KalturaUserRole();
  406. $newRole->name = 'Test role with ';
  407. $newRole->permissionNames = UserRole::ALL_PARTNER_PERMISSIONS_WILDCARD;
  408. $addedRole = $this->addRoleWrap($newRole);
  409. $getRole = $this->client->userRole->get($addedRole->id);
  410. $this->assertEquals($addedRole->permissionNames, $getRole->permissionNames);
  411. $c = new Criteria();
  412. $c->addAnd(PermissionPeer::PARTNER_ID, array(self::TEST_PARTNER_ID, PartnerPeer::GLOBAL_PARTNER), Criteria::IN);
  413. $c->addAnd(PermissionPeer::TYPE, PermissionType::NORMAL, Criteria::EQUAL);
  414. $c->addAnd(PermissionPeer::STATUS, PermissionStatus::ACTIVE, Criteria::EQUAL);
  415. $allPartnerPermissions = PermissionPeer::doSelect($c);
  416. $returnedPermissions = explode(',', trim($getRole->permissionNames,','));
  417. $this->assertEquals(count($allPartnerPermissions), count($returnedPermissions));
  418. foreach ($allPartnerPermissions as $permission)
  419. {
  420. $this->assertTrue(in_array($permission->getName(), $returnedPermissions));
  421. }
  422. }
  423. public function testListAction()
  424. {
  425. $this->startSession(KalturaSessionType::ADMIN, $this->getDbPartner()->getAdminUserId());
  426. // get list
  427. $listResult = $this->client->userRole->listAction();
  428. $roleList = $listResult->objects;
  429. $returnedRoleNames = array();
  430. foreach ($roleList as $role)
  431. {
  432. $returnedRoleNames[] = $role->name;
  433. }
  434. // check that total count is right
  435. $this->assertGreaterThan(0, count($roleList));
  436. $this->assertEquals(count($roleList), $listResult->totalCount);
  437. // check that only partner 0 and current partner roles are returned
  438. foreach ($roleList as $role)
  439. {
  440. if ($role->partnerId != self::TEST_PARTNER_ID && $role->partnerId != PartnerPeer::GLOBAL_PARTNER) {
  441. $this->fail('List returned role id ['.$role->id.'] of partner ['.$role->partnerId.'] instead of partner ['.self::TEST_PARTNER_ID.']');
  442. }
  443. }
  444. // check that all partner 0 roles are returned
  445. $c = new Criteria();
  446. $c->addAnd(UserRolePeer::PARTNER_ID, PartnerPeer::GLOBAL_PARTNER, Criteria::EQUAL);
  447. $partner0Roles = UserRolePeer::doSelect($c);
  448. foreach ($partner0Roles as $role)
  449. {
  450. $this->assertTrue(in_array($role->getName(), $returnedRoleNames));
  451. }
  452. //TODO: test filters ?
  453. }
  454. public function testUpdateAction()
  455. {
  456. $this->startSession(KalturaSessionType::ADMIN, $this->getDbPartner()->getAdminUserId());
  457. // failure to update partner 0 roles
  458. $c = new Criteria();
  459. $c->addAnd(UserRolePeer::PARTNER_ID, PartnerPeer::GLOBAL_PARTNER, Criteria::EQUAL);
  460. $partner0Roles = UserRolePeer::doSelect($c);
  461. for ($i=1; $i<4; $i++)
  462. {
  463. $randId = rand(0, count($partner0Roles)-1);
  464. $exceptionThrown = false;
  465. $updateRole = new KalturaUserRole();
  466. $updateRole->name = uniqid();
  467. try { $this->client->userRole->update($partner0Roles[$randId]->getId(), $updateRole); }
  468. catch (Exception $e) { $exceptionThrown = $e; }
  469. $this->checkException($exceptionThrown, 'INVALID_OBJECT_ID');
  470. }
  471. // add a new role to test with
  472. $newRole = new KalturaUserRole();
  473. $newRole->name = uniqid();
  474. $addedRole = $this->addRoleWrap($newRole);
  475. // failure to add a permisison which the partner does not have
  476. $updateRole = new KalturaUserRole();
  477. $updateRole->permissionNames = PermissionName::BATCH_BASE;
  478. $exceptionThrown = false;
  479. try { $addedRole = $this->client->userRole->update($addedRole->id, $updateRole); }
  480. catch (Exception $e) { $exceptionThrown = $e; }
  481. $this->checkException($exceptionThrown, 'PERMISSION_NOT_FOUND');
  482. // success adding and removing valid permissions
  483. $updateRole = new KalturaUserRole();
  484. $updateRole->permissionNames = PermissionName::ACCOUNT_BASE.','.PermissionName::CONTENT_MANAGE_EMBED_CODE;
  485. $resultRole = $this->client->userRole->update($addedRole->id, $updateRole);
  486. $this->assertEquals($updateRole->permissionNames, $resultRole->permissionNames);
  487. // replace permissions test - verify that old permissions are no more returned
  488. $updateRole = new KalturaUserRole();
  489. $updateRole->permissionNames = PermissionName::CONTENT_INGEST_BULK_UPLOAD.','.PermissionName::CUSTOM_DATA_PROFILE_DELETE;
  490. $resultRole = $this->client->userRole->update($addedRole->id, $updateRole);
  491. $this->assertEquals($updateRole->permissionNames, $resultRole->permissionNames);
  492. // success updating name, description and status
  493. $updateRole = new KalturaUserRole();
  494. $updateRole->name = uniqid();
  495. $updateRole->description = uniqid();
  496. $updateRole->status = KalturaUserRoleStatus::BLOCKED;
  497. $resultRole = $this->client->userRole->update($addedRole->id, $updateRole);
  498. $this->assertEquals($updateRole->name, $resultRole->name);
  499. $this->assertEquals($updateRole->description, $resultRole->description);
  500. $this->assertEquals($updateRole->status, $resultRole->status);
  501. // failure to update partner id
  502. $updateRole = new KalturaUserRole();
  503. $updateRole->partnerId = rand(100, 300);
  504. $exceptionThrown = false;
  505. try { $addedRole = $this->client->userRole->update($addedRole->id, $updateRole); }
  506. catch (Exception $e) { $exceptionThrown = $e; }
  507. $this->checkException($exceptionThrown, 'PROPERTY_VALIDATION_NOT_UPDATABLE');
  508. // failure to update role id
  509. $updateRole = new KalturaUserRole();
  510. $updateRole->id = rand(1, 1000);
  511. $exceptionThrown = false;
  512. try { $addedRole = $this->client->userRole->update($addedRole->id, $updateRole); }
  513. catch (Exception $e) { $exceptionThrown = $e; }
  514. $this->checkException($exceptionThrown, 'PROPERTY_VALIDATION_NOT_UPDATABLE');
  515. // failure to update createdAt
  516. $updateRole = new KalturaUserRole();
  517. $updateRole->createdAt = time();
  518. $exceptionThrown = false;
  519. try { $addedRole = $this->client->userRole->update($addedRole->id, $updateRole); }
  520. catch (Exception $e) { $exceptionThrown = $e; }
  521. $this->checkException($exceptionThrown, 'PROPERTY_VALIDATION_NOT_UPDATABLE');
  522. // failure to update updatedAt
  523. $updateRole = new KalturaUserRole();
  524. $updateRole->updatedAt = time();
  525. $exceptionThrown = false;
  526. try { $addedRole = $this->client->userRole->update($addedRole->id, $updateRole); }
  527. catch (Exception $e) { $exceptionThrown = $e; }
  528. $this->checkException($exceptionThrown, 'PROPERTY_VALIDATION_NOT_UPDATABLE');
  529. //TODO: verify that only given parameters were updated and not other parameters
  530. }
  531. }