X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fapi%2Fv4%2FAction%2FBasicActionsTest.php;h=b71f2683a787b55783c305ef56643eb8f9d574ee;hb=7392872b837fd644d37c5db75cbaea302b40988d;hp=27ba63953ad514af65ab9ca2a821eaa4e1c34420;hpb=e7684f068748a99bd01bae52fa5ac717f9beb1bb;p=civicrm-core.git diff --git a/tests/phpunit/api/v4/Action/BasicActionsTest.php b/tests/phpunit/api/v4/Action/BasicActionsTest.php index 27ba63953a..b71f2683a7 100644 --- a/tests/phpunit/api/v4/Action/BasicActionsTest.php +++ b/tests/phpunit/api/v4/Action/BasicActionsTest.php @@ -45,7 +45,9 @@ class BasicActionsTest extends UnitTestCase { MockBasicEntity::update()->addWhere('id', '=', $id2)->addValue('foo', 'new')->execute(); $result = MockBasicEntity::get()->addOrderBy('id', 'DESC')->setLimit(1)->execute(); - $this->assertCount(1, $result); + // The object's count() method will account for all results, ignoring limit, while the array results are limited + $this->assertCount(2, $result); + $this->assertCount(1, (array) $result); $this->assertEquals('new', $result->first()['foo']); $result = MockBasicEntity::save() @@ -145,23 +147,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 +257,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()); + } + }