Просмотр исходного кода

Subject model: added findByDistinguishedName(dn)

Fabian Peter Hammerle 8 лет назад
Родитель
Сommit
7004c04829
2 измененных файлов с 27 добавлено и 0 удалено
  1. 5 0
      Subject.php
  2. 22 0
      tests/SubjectTest.php

+ 5 - 0
Subject.php

@@ -82,4 +82,9 @@ class Subject extends \yii\db\ActiveRecord
         // TODO: update related record
         // $this->getRelatedRecords()['identity'] = $identity;
     }
+
+    public function findByDistinguishedName($dn)
+    {
+        return self::findOne(['distinguished_name' => $dn]);
+    }
 }

+ 22 - 0
tests/SubjectTest.php

@@ -110,4 +110,26 @@ class SubjectTest extends TestCase
         $this->setExpectedException(\TypeError::class);
         new Subject(new DummyUser);
     }
+
+    public function testFindByDN()
+    {
+        $a1 = new Subject($this->alice, 'CN=Alice,C=AT');
+        $a2 = new Subject($this->alice, 'CN=Alice,O=Office,C=AT');
+        $b = new Subject($this->bob, 'CN=Bob,C=AT');
+        $a1->save();
+        $a2->save();
+        $b->save();
+
+        $this->assertEquals(
+            $a1->id,
+            Subject::findByDistinguishedName('CN=Alice,C=AT')->id
+        );
+        $this->assertEquals(
+            $a2->id,
+            Subject::findByDistinguishedName('CN=Alice,O=Office,C=AT')->id
+        );
+        $this->assertNull(
+            Subject::findByDistinguishedName('CN=Bob,O=Office,C=AT')
+        );
+    }
 }