SubjectTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace fphammerle\yii2\auth\clientcert\tests;
  3. use \fphammerle\helpers\ArrayHelper;
  4. use \fphammerle\yii2\auth\clientcert\Subject;
  5. use \fphammerle\yii2\auth\clientcert\migrations;
  6. class SubjectTest extends TestCase
  7. {
  8. protected $alice;
  9. protected $bob;
  10. protected function setUp()
  11. {
  12. $this->mockApplication();
  13. (new migrations\CreateSubjectTable)->up();
  14. $this->alice = new models\User('alice');
  15. $this->bob = new models\User('bob');
  16. $this->assertTrue($this->alice->save());
  17. $this->assertTrue($this->bob->save());
  18. }
  19. public function testCreateModel()
  20. {
  21. (new Subject($this->alice, 'CN=Alice,C=AT'))->save();
  22. (new Subject($this->alice, 'CN=Alice,O=Office,C=AT'))->save();
  23. (new Subject($this->bob, 'CN=Bob,C=AT'))->save();
  24. $subjects = ArrayHelper::map(
  25. Subject::find()->all(),
  26. function($s) { return $s->getAttributes(); }
  27. );
  28. $this->assertEquals(3, sizeof($subjects));
  29. ArrayHelper::map(
  30. [['id' => 1, 'identity_id' => $this->alice->id, 'distinguished_name' => 'CN=Alice,C=AT'],
  31. ['id' => 2, 'identity_id' => $this->alice->id, 'distinguished_name' => 'CN=Alice,O=Office,C=AT'],
  32. ['id' => 3, 'identity_id' => $this->bob->id, 'distinguished_name' => 'CN=Bob,C=AT']],
  33. function($a) use ($subjects) {
  34. $this->assertContains($a, $subjects);
  35. }
  36. );
  37. }
  38. public function testDNUnique()
  39. {
  40. $this->assertTrue((new Subject($this->alice, 'CN=Alice,C=AT'))->save());
  41. $this->assertTrue((new Subject($this->bob, 'CN=Bob,C=AT'))->save());
  42. $dup = new Subject($this->alice, 'CN=Alice,C=AT');
  43. $this->assertFalse($dup->save());
  44. $this->assertEquals(1, sizeof($dup->getErrors()));
  45. $this->assertEquals(1, sizeof($dup->getErrors('distinguished_name')));
  46. }
  47. public function testGetIdentity()
  48. {
  49. $s = new Subject;
  50. $this->assertNull($s->identity);
  51. $s = new Subject($this->alice, 'CN=Alice,C=AT');
  52. $this->assertEquals($this->alice->id, $s->identity_id);
  53. $this->assertInstanceOf(models\User::className(), $s->identity);
  54. $this->assertEquals($this->alice->id, $s->identity->id);
  55. $s->identity_id = $this->bob->id;
  56. $s->save();
  57. $s = Subject::findOne(['identity_id' => $this->bob->id]);
  58. $this->assertEquals($this->bob->id, $s->identity_id);
  59. $this->assertInstanceOf(models\User::className(), $s->identity);
  60. $this->assertEquals($this->bob->id, $s->identity->id);
  61. }
  62. public function testSetIdentity()
  63. {
  64. $s = new Subject($this->alice, 'CN=Alice,C=AT');
  65. $this->assertInstanceOf(models\User::className(), $s->identity);
  66. $this->assertEquals($this->alice->id, $s->identity->id);
  67. $s->identity = $this->bob;
  68. $this->assertEquals($this->bob->id, $s->identity_id);
  69. // TODO: $this->assertEquals($this->bob->id, $s->identity->id);
  70. $s->save();
  71. $s = Subject::findOne(['identity_id' => $this->bob->id]);
  72. $this->assertEquals($this->bob->id, $s->identity_id);
  73. $this->assertEquals($this->bob->id, $s->identity->id);
  74. $s->identity = null;
  75. $this->assertNull($s->identity_id);
  76. }
  77. }