4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace api\v
4\Action
;
24 use api\v
4\UnitTestCase
;
29 class IndexTest
extends UnitTestCase
{
31 public function testIndex() {
32 // Results indexed by name
33 $resultByName = civicrm_api4('Activity', 'getActions', [], 'name');
34 $this->assertInstanceOf('Civi\Api4\Generic\Result', $resultByName);
35 $this->assertEquals('get', $resultByName['get']['name']);
37 // Get result at index 0
38 $firstResult = civicrm_api4('Activity', 'getActions', [], 0);
39 $this->assertInstanceOf('Civi\Api4\Generic\Result', $firstResult);
40 $this->assertArrayHasKey('name', $firstResult);
42 $this->assertEquals($resultByName->first(), (array) $firstResult);
45 public function testBadIndexInt() {
48 civicrm_api4('Activity', 'getActions', [], 99);
50 catch (\API_Exception
$e) {
51 $error = $e->getMessage();
53 $this->assertContains('not found', $error);
56 public function testBadIndexString() {
59 civicrm_api4('Activity', 'getActions', [], 'xyz');
61 catch (\API_Exception
$e) {
62 $error = $e->getMessage();
64 $this->assertContains('not found', $error);
67 public function testIndexWithSelect() {
68 $result = civicrm_api4('Activity', 'getFields', ['select' => ['title'], 'where' => [['name', '=', 'subject']]], 'name');
69 $this->assertEquals(['subject' => ['title' => 'Subject']], (array) $result);
72 public function testArrayIndex() {
74 $result = civicrm_api4('Activity', 'getFields', ['where' => [['name', '=', 'subject']]], ['name' => 'title']);
75 $this->assertEquals(['subject' => 'Subject'], (array) $result);
78 $result = civicrm_api4('Activity', 'getFields', ['where' => [['name', '=', 'subject']]], ['title']);
79 $this->assertEquals(['Subject'], (array) $result);