dev/cor#3874 Added test cases for failing relationship creation
authorShane Bill <shane@beesonabike.com>
Mon, 24 Oct 2022 17:08:08 +0000 (14:08 -0300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 25 Nov 2022 01:23:38 +0000 (14:23 +1300)
tests/phpunit/api/v4/Entity/RelationshipTest.php

index 5a6b13c18e57a624e5242d8cd0cfcd47bdf4b882..f3d9f4f22c65bf102e3b34748faba9d66af2e1d9 100644 (file)
@@ -77,4 +77,62 @@ class RelationshipTest extends Api4TestBase implements TransactionalInterface {
     $this->assertEquals($relationship['modified_date'], $cacheRecords[$c2]['relationship_modified_date']);
   }
 
+  public function testRelationshipDisableCreate(): void {
+    $today = new \DateTime('today');
+    $future = new \DateTime('today');
+    $future->add(new \DateInterval('P1Y'));
+
+    $c1 = Contact::create(FALSE)->addValue('first_name', '1')->execute()->first()['id'];
+    $c2 = Contact::create(FALSE)->addValue('first_name', '2')->execute()->first()['id'];
+    $relationship = Relationship::create(FALSE)
+      ->setValues([
+        'contact_id_a' => $c1,
+        'contact_id_b' => $c2,
+        'start_date' => $today->format('Y-m-d'),
+        'end_date' => $future->format('Y-m-d'),
+        'relationship_type_id' => 1,
+        'description' => "Wow, we're related!",
+        'is_permission_a_b' => 1,
+        'is_permission_b_a:name' => 'View only',
+      ])->execute()->first();
+    $relationship = Relationship::get(FALSE)
+      ->addWhere('id', '=', $relationship['id'])
+      ->execute()->first();
+    $relationshipDisable = Relationship::update(FALSE)
+      ->addWhere('id', '=', $relationship['id'])
+      ->addValue('is_active', FALSE)
+      ->execute()->first();
+    $relationship2 = Relationship::create(FALSE)
+      ->setValues([
+        'contact_id_a' => $c1,
+        'contact_id_b' => $c2,
+        'start_date' => $today->format('Y-m-d'),
+        'end_date' => $future->format('Y-m-d'),
+        'relationship_type_id' => 1,
+        'description' => "Wow, we're related!",
+        'is_permission_a_b' => 1,
+        'is_permission_b_a:name' => 'View only',
+      ])->execute()->first();
+
+    $cacheRecords = RelationshipCache::get(FALSE)
+      ->addWhere('near_contact_id', 'IN', [$c1])
+      ->addWhere('is_active', '=', FALSE)
+      ->addSelect('near_contact_id', 'orientation', 'description', 'start_date', 'end_date')
+      ->execute()->indexBy('near_contact_id');
+    $this->assertCount(1, $cacheRecords);
+    $this->assertEquals(FALSE, $cacheRecords[$c1]['is_active']);
+    $this->assertEquals($today, $cachedRecords[$c1]['start_date']);
+    $this->assertEquals($future, $cachedRecords[$c1]['end_date']);
+
+    $cacheRecords = RelationshipCache::get(FALSE)
+      ->addWhere('near_contact_id', 'IN', [$c1])
+      ->addWhere('is_active', '=', TRUE)
+      ->addSelect('near_contact_id', 'orientation', 'description', 'start_date', 'end_date')
+      ->execute()->indexBy('near_contact_id');
+    $this->assertCount(1, $cacheRecords);
+    $this->assertEquals(TRUE, $cacheRecords[$c1]['is_active']);
+    $this->assertEquals($today, $cachedRecords[$c1]['start_date']);
+    $this->assertEquals($future, $cachedRecords[$c1]['end_date']);
+  }
+
 }