Sfoglia il codice sorgente

Subject model: added getIdentity()

Fabian Peter Hammerle 8 anni fa
parent
commit
709d364504
2 ha cambiato i file con 25 aggiunte e 7 eliminazioni
  1. 7 7
      Subject.php
  2. 18 0
      tests/SubjectTest.php

+ 7 - 7
Subject.php

@@ -57,11 +57,11 @@ class Subject extends \yii\db\ActiveRecord
         return array_pop($keys);
     }
 
-    // public function getIdentity()
-    // {
-    //     return $this->hasOne(
-    //         self::getIdentityClass(),
-    //         [Subject::getIdentityIdSchema()->name => 'identity_id']
-    //     );
-    // }
+    public function getIdentity()
+    {
+        return $this->hasOne(
+            self::getIdentityClass(),
+            [Subject::getIdentityIdSchema()->name => 'identity_id']
+        );
+    }
 }

+ 18 - 0
tests/SubjectTest.php

@@ -51,4 +51,22 @@ class SubjectTest extends TestCase
         $this->assertEquals(1, sizeof($dup->getErrors()));
         $this->assertEquals(1, sizeof($dup->getErrors('distinguished_name')));
     }
+
+    public function testGetIdentity()
+    {
+        $s = new Subject;
+        $this->assertNull($s->identity);
+
+        $s = new Subject($this->alice, 'CN=Alice,C=AT');
+        $this->assertEquals($this->alice->id, $s->identity_id);
+        $this->assertInstanceOf(models\User::className(), $s->identity);
+        $this->assertEquals($this->alice->id, $s->identity->id);
+
+        $s->identity_id = $this->bob->id;
+        $s->save();
+        $s = Subject::findOne(['identity_id' => $this->bob->id]);
+        $this->assertEquals($this->bob->id, $s->identity_id);
+        $this->assertInstanceOf(models\User::className(), $s->identity);
+        $this->assertEquals($this->bob->id, $s->identity->id);
+    }
 }