Merge pull request #26352 from colemanw/groupChildren
authorAidan Saunders <aidan.saunders@squiffle.uk>
Tue, 30 May 2023 10:57:08 +0000 (11:57 +0100)
committerGitHub <noreply@github.com>
Tue, 30 May 2023 10:57:08 +0000 (11:57 +0100)
APIv4 - Add metadata to `Group.children` column and `GroupNesting` entity

1  2 
tests/phpunit/api/v4/Entity/GroupTest.php

index 09935b6aa670100415a290d411e8d66c02063b32,5e987e7a66b6c48e59a2f9b59fa8db53bd26436d..b811c3394e046b8221d4450f00055380e26ed367
@@@ -144,61 -111,33 +148,88 @@@ class GroupTest extends Api4TestBase 
        ->addSelect('parents:label')
        ->execute()->single();
      $this->assertEquals([$parent1['title'], $parent2['title']], $get['parents:label']);
+     $get = Group::get(FALSE)
+       ->addWhere('id', '=', $parent1['id'])
+       ->addSelect('children')
+       ->execute()->single();
+     $this->assertEquals([$child1['id']], $get['children']);
+     $get = Group::get(FALSE)
+       ->addWhere('id', '=', $parent2['id'])
+       ->addSelect('children:label')
+       ->execute()->single();
+     $this->assertEquals([$child1['title'], $child2['title']], $get['children:label']);
+     $joined = Group::get(FALSE)
+       ->addWhere('id', 'IN', [$parent1['id'], $parent2['id'], $child1['id'], $child2['id']])
+       ->addSelect('id', 'child_group.id', 'child_group.title')
+       ->addJoin('Group AS child_group', 'INNER', 'GroupNesting', ['id', '=', 'child_group.parent_group_id'])
+       ->addOrderBy('id')
+       ->execute();
+     $this->assertCount(3, $joined);
+     $this->assertEquals($parent1['id'], $joined[0]['id']);
+     $this->assertEquals($child1['title'], $joined[0]['child_group.title']);
+     $this->assertEquals($parent2['id'], $joined[1]['id']);
+     $this->assertEquals($child1['title'], $joined[1]['child_group.title']);
+     $this->assertEquals($parent2['id'], $joined[2]['id']);
+     $this->assertEquals($child2['title'], $joined[2]['child_group.title']);
    }
  
 +  public function testAddRemoveParents() {
 +    $group1 = Group::create(FALSE)
 +      ->addValue('title', uniqid())
 +      ->execute()->single();
 +    $parent1 = Group::create(FALSE)
 +      ->addValue('title', uniqid())
 +      ->execute()->single();
 +    $parent2 = Group::create(FALSE)
 +      ->addValue('title', uniqid())
 +      ->execute()->single();
 +
 +    // ensure self is not added as parent
 +    Group::update(FALSE)
 +      ->addValue('parents', [$group1['id']])
 +      ->addWhere('id', '=', $group1['id'])
 +      ->execute();
 +    $get = Group::get(FALSE)
 +      ->addWhere('id', '=', $group1['id'])
 +      ->addSelect('parents')
 +      ->execute()->single();
 +    $this->assertEmpty($get['parents']);
 +
 +    Group::update(FALSE)
 +      ->addValue('parents', [$parent1['id'], $parent2['id'], $group1['id']])
 +      ->addWhere('id', '=', $group1['id'])
 +      ->execute();
 +    $get = Group::get(FALSE)
 +      ->addWhere('id', '=', $group1['id'])
 +      ->addSelect('parents')
 +      ->execute()->single();
 +    $this->assertEquals([$parent1['id'], $parent2['id']], $get['parents']);
 +
 +    // ensure adding something else doesn't impact parents
 +    Group::update(FALSE)
 +      ->addValue('title', uniqid())
 +      ->addWhere('id', '=', $group1['id'])
 +      ->execute();
 +    $get = Group::get(FALSE)
 +      ->addWhere('id', '=', $group1['id'])
 +      ->addSelect('parents')
 +      ->execute()->single();
 +    $this->assertEquals([$parent1['id'], $parent2['id']], $get['parents']);
 +
 +    // ensure removing parent is working
 +    Group::update(FALSE)
 +      ->addValue('parents', [$parent2['id']])
 +      ->addWhere('id', '=', $group1['id'])
 +      ->execute();
 +    $get = Group::get(FALSE)
 +      ->addWhere('id', '=', $group1['id'])
 +      ->addSelect('parents')
 +      ->execute()->single();
 +    $this->assertEquals([$parent2['id']], $get['parents']);
 +  }
 +
  }