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
19 namespace api\v
4\Entity
;
21 use Civi\Api4\Contact
;
22 use api\v
4\Api4TestBase
;
23 use Civi\Api4\EntityTag
;
25 use Civi\Test\TransactionalInterface
;
30 class TagTest
extends Api4TestBase
implements TransactionalInterface
{
32 public function testTagFilter() {
33 // Ensure bypassing permissions works correctly by giving none to the logged-in user
34 $this->createLoggedInUser();
35 \CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= [];
37 $conTag = Tag
::create(FALSE)
38 ->addValue('name', uniqid('con'))
39 ->addValue('used_for', 'civicrm_contact')
40 ->addValue('color', '#cccccc')
42 $tagChild = Tag
::create(FALSE)
43 ->addValue('name', uniqid('child'))
44 ->addValue('parent_id', $conTag['id'])
46 $tagSubChild = Tag
::create(FALSE)
47 ->addValue('name', uniqid('child'))
48 ->addValue('parent_id', $tagChild['id'])
50 $tagSet = Tag
::create(FALSE)
51 ->addValue('name', uniqid('set'))
52 ->addValue('used_for', 'civicrm_contact')
53 ->addValue('is_tagset', TRUE)
55 $setChild = Tag
::create(FALSE)
56 ->addValue('name', uniqid('child'))
57 ->addValue('parent_id', $tagSet['id'])
60 $contact1 = Contact
::create(FALSE)
62 $contact2 = Contact
::create(FALSE)
64 EntityTag
::create(FALSE)
65 ->addValue('entity_id', $contact1['id'])
66 ->addValue('entity_table', 'civicrm_contact')
67 ->addValue('tag_id', $tagSubChild['id'])
69 EntityTag
::create(FALSE)
70 ->addValue('entity_id', $contact2['id'])
71 ->addValue('entity_table', 'civicrm_contact')
72 ->addValue('tag_id', $setChild['id'])
75 $shouldReturnContact1 = Contact
::get(FALSE)
77 ->addWhere('tags:name', 'IN', [$conTag['name']])
79 $this->assertCount(1, $shouldReturnContact1);
80 $this->assertEquals($contact1['id'], $shouldReturnContact1->first()['id']);
82 $shouldReturnContact2 = Contact
::get(FALSE)
84 ->addWhere('tags', 'IN', [$setChild['id']])
86 $this->assertCount(1, $shouldReturnContact2);
87 $this->assertEquals($contact2['id'], $shouldReturnContact2->first()['id']);