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\UnitTestCase
;
23 use Civi\Api4\EntityTag
;
25 use Civi\Test\TransactionalInterface
;
30 class TagTest
extends UnitTestCase
implements TransactionalInterface
{
32 public function testTagFilter() {
33 $conTag = Tag
::create(FALSE)
34 ->addValue('name', uniqid('con'))
35 ->addValue('used_for', 'civicrm_contact')
36 ->addValue('color', '#cccccc')
38 $tagChild = Tag
::create(FALSE)
39 ->addValue('name', uniqid('child'))
40 ->addValue('parent_id', $conTag['id'])
42 $tagSubChild = Tag
::create(FALSE)
43 ->addValue('name', uniqid('child'))
44 ->addValue('parent_id', $tagChild['id'])
46 $tagSet = Tag
::create(FALSE)
47 ->addValue('name', uniqid('set'))
48 ->addValue('used_for', 'civicrm_contact')
49 ->addValue('is_tagset', TRUE)
51 $setChild = Tag
::create(FALSE)
52 ->addValue('name', uniqid('child'))
53 ->addValue('parent_id', $tagSet['id'])
56 $contact1 = Contact
::create(FALSE)
58 $contact2 = Contact
::create(FALSE)
60 EntityTag
::create(FALSE)
61 ->addValue('entity_id', $contact1['id'])
62 ->addValue('entity_table', 'civicrm_contact')
63 ->addValue('tag_id', $tagSubChild['id'])
65 EntityTag
::create(FALSE)
66 ->addValue('entity_id', $contact2['id'])
67 ->addValue('entity_table', 'civicrm_contact')
68 ->addValue('tag_id', $setChild['id'])
71 $shouldReturnContact1 = Contact
::get(FALSE)
73 ->addWhere('tags:name', 'IN', [$conTag['name']])
75 $this->assertCount(1, $shouldReturnContact1);
76 $this->assertEquals($contact1['id'], $shouldReturnContact1->first()['id']);
78 $shouldReturnContact2 = Contact
::get(FALSE)
80 ->addWhere('tags', 'IN', [$setChild['id']])
82 $this->assertCount(1, $shouldReturnContact2);
83 $this->assertEquals($contact2['id'], $shouldReturnContact2->first()['id']);