->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']);
+ }
+
}