ContextTest.php 6.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Muzich\CoreBundle\Tests\Context;
  3. use Muzich\CoreBundle\Entity\User;
  4. use Muzich\CoreBundle\Security\Context as SecurityContext;
  5. class ContextTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testActionsWithNotConfirmedEmailUser()
  8. {
  9. $user_not_confirmed_email = new User();
  10. $user_not_confirmed_email->setEmailConfirmed(false);
  11. $secutiry_context = new SecurityContext($user_not_confirmed_email);
  12. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD));
  13. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_NOTE));
  14. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_COMMENT_ALERT));
  15. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ALERT));
  16. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_TAG_ADD));
  17. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION));
  18. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_GROUP_ADD));
  19. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_COMMENT_ADD));
  20. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_USER_FOLLOW));
  21. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES));
  22. $this->assertEquals('UserEmailNotConfirmed', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_NOTE));
  23. $this->assertEquals('UserEmailNotConfirmed', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION));
  24. $this->assertEquals('UserEmailNotConfirmed', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_USER_FOLLOW));
  25. $this->assertEquals('UserEmailNotConfirmed', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES));
  26. }
  27. public function testActionsWithConfirmedEmailUser()
  28. {
  29. $user_email_confirmed = new User();
  30. $user_email_confirmed->setEmailConfirmed(true);
  31. $secutiry_context = new SecurityContext($user_email_confirmed);
  32. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD));
  33. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_NOTE));
  34. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_COMMENT_ALERT));
  35. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ALERT));
  36. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_TAG_ADD));
  37. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_COMMENT_ADD));
  38. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION));
  39. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_GROUP_ADD));
  40. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_USER_FOLLOW));
  41. $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES));
  42. $this->assertFalse($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_NOTE));
  43. $this->assertFalse($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION));
  44. $this->assertFalse($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_USER_FOLLOW));
  45. $this->assertFalse($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES));
  46. }
  47. public function testCanMakeActionsWithNotConnectedUser()
  48. {
  49. $secutiry_context = new SecurityContext('anon.');
  50. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD));
  51. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_NOTE));
  52. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_COMMENT_ALERT));
  53. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ALERT));
  54. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_TAG_ADD));
  55. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_COMMENT_ADD));
  56. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION));
  57. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_GROUP_ADD));
  58. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_USER_FOLLOW));
  59. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES));
  60. $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_GET_FAVORITES_TAGS));
  61. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_ELEMENT_ADD));
  62. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_ELEMENT_NOTE));
  63. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_COMMENT_ALERT));
  64. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_ELEMENT_ALERT));
  65. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_TAG_ADD));
  66. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_COMMENT_ADD));
  67. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_COMMENT_ADD));
  68. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION));
  69. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_GROUP_ADD));
  70. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES));
  71. $this->assertEquals('UserNotConnected', $secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_CANT_MAKE, SecurityContext::ACTION_GET_FAVORITES_TAGS));
  72. }
  73. }