X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fapi%2Fv4%2FAction%2FBasicActionsTest.php;h=daf2b3470eab21374bdc9d995b1a503772a0aeb4;hb=a4499ec59bb340e7718ff2e91f5b183ed585021c;hp=27ba63953ad514af65ab9ca2a821eaa4e1c34420;hpb=5fa8d81b835dbd4d2bdc2f539fdd889fc7da0aad;p=civicrm-core.git diff --git a/tests/phpunit/api/v4/Action/BasicActionsTest.php b/tests/phpunit/api/v4/Action/BasicActionsTest.php index 27ba63953a..daf2b3470e 100644 --- a/tests/phpunit/api/v4/Action/BasicActionsTest.php +++ b/tests/phpunit/api/v4/Action/BasicActionsTest.php @@ -145,23 +145,43 @@ class BasicActionsTest extends UnitTestCase { public function testGetFields() { $getFields = MockBasicEntity::getFields()->execute()->indexBy('name'); - $this->assertCount(6, $getFields); + $this->assertCount(7, $getFields); $this->assertEquals('Id', $getFields['id']['title']); // Ensure default data type is "String" when not specified $this->assertEquals('String', $getFields['color']['data_type']); // Getfields should default to loadOptions = false and reduce them to bool $this->assertTrue($getFields['group']['options']); + $this->assertTrue($getFields['fruit']['options']); $this->assertFalse($getFields['id']['options']); - // Now load options + // Load simple options $getFields = MockBasicEntity::getFields() - ->addWhere('name', '=', 'group') + ->addWhere('name', 'IN', ['group', 'fruit']) ->setLoadOptions(TRUE) ->execute()->indexBy('name'); - $this->assertCount(1, $getFields); + $this->assertCount(2, $getFields); $this->assertArrayHasKey('one', $getFields['group']['options']); + // Complex options should be reduced to simple array + $this->assertArrayHasKey(1, $getFields['fruit']['options']); + $this->assertEquals('Banana', $getFields['fruit']['options'][3]); + + // Load complex options + $getFields = MockBasicEntity::getFields() + ->addWhere('name', 'IN', ['group', 'fruit']) + ->setLoadOptions(['id', 'name', 'label', 'color']) + ->execute()->indexBy('name'); + + // Simple options should be expanded to non-assoc array + $this->assertCount(2, $getFields); + $this->assertEquals('one', $getFields['group']['options'][0]['id']); + $this->assertEquals('First', $getFields['group']['options'][0]['name']); + $this->assertEquals('First', $getFields['group']['options'][0]['label']); + $this->assertFalse(isset($getFields['group']['options'][0]['color'])); + // Complex options should give all requested properties + $this->assertEquals('Banana', $getFields['fruit']['options'][2]['label']); + $this->assertEquals('yellow', $getFields['fruit']['options'][2]['color']); } public function testItemsToGet() { @@ -235,4 +255,42 @@ class BasicActionsTest extends UnitTestCase { $this->assertEquals(['shape', 'size', 'weight'], array_keys($result)); } + public function testPseudoconstantMatch() { + MockBasicEntity::delete()->addWhere('id', '>', 0)->execute(); + + $records = [ + ['group:label' => 'First', 'shape' => 'round', 'fruit:name' => 'banana'], + ['group:name' => 'Second', 'shape' => 'square', 'fruit:label' => 'Pear'], + ]; + MockBasicEntity::save()->setRecords($records)->execute(); + + $results = MockBasicEntity::get() + ->addSelect('*', 'group:label', 'group:name', 'fruit:name', 'fruit:color', 'fruit:label') + ->addOrderBy('fruit:color', "DESC") + ->execute(); + + $this->assertEquals('round', $results[0]['shape']); + $this->assertEquals('one', $results[0]['group']); + $this->assertEquals('First', $results[0]['group:label']); + $this->assertEquals('First', $results[0]['group:name']); + $this->assertEquals(3, $results[0]['fruit']); + $this->assertEquals('Banana', $results[0]['fruit:label']); + $this->assertEquals('banana', $results[0]['fruit:name']); + $this->assertEquals('yellow', $results[0]['fruit:color']); + + // Reverse order + $results = MockBasicEntity::get() + ->addOrderBy('fruit:color') + ->execute(); + $this->assertEquals('two', $results[0]['group']); + + // Cannot match to a non-unique option property like :color on create + try { + MockBasicEntity::create()->addValue('fruit:color', 'yellow')->execute(); + } + catch (\API_Exception $createError) { + } + $this->assertContains('Illegal expression', $createError->getMessage()); + } + }