Merge pull request #20570 from eileenmcnaughton/return
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / TagTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19 namespace api\v4\Entity;
20
21 use Civi\Api4\Contact;
22 use api\v4\UnitTestCase;
23 use Civi\Api4\EntityTag;
24 use Civi\Api4\Tag;
25 use Civi\Test\TransactionalInterface;
26
27 /**
28 * @group headless
29 */
30 class TagTest extends UnitTestCase implements TransactionalInterface {
31
32 public function testTagFilter() {
33 $conTag = Tag::create(FALSE)
34 ->addValue('name', uniqid('con'))
35 ->addValue('used_for', 'civicrm_contact')
36 ->addValue('color', '#cccccc')
37 ->execute()->first();
38 $tagChild = Tag::create(FALSE)
39 ->addValue('name', uniqid('child'))
40 ->addValue('parent_id', $conTag['id'])
41 ->execute()->first();
42 $tagSubChild = Tag::create(FALSE)
43 ->addValue('name', uniqid('child'))
44 ->addValue('parent_id', $tagChild['id'])
45 ->execute()->first();
46 $tagSet = Tag::create(FALSE)
47 ->addValue('name', uniqid('set'))
48 ->addValue('used_for', 'civicrm_contact')
49 ->addValue('is_tagset', TRUE)
50 ->execute()->first();
51 $setChild = Tag::create(FALSE)
52 ->addValue('name', uniqid('child'))
53 ->addValue('parent_id', $tagSet['id'])
54 ->execute()->first();
55
56 $contact1 = Contact::create(FALSE)
57 ->execute()->first();
58 $contact2 = Contact::create(FALSE)
59 ->execute()->first();
60 EntityTag::create(FALSE)
61 ->addValue('entity_id', $contact1['id'])
62 ->addValue('entity_table', 'civicrm_contact')
63 ->addValue('tag_id', $tagSubChild['id'])
64 ->execute();
65 EntityTag::create(FALSE)
66 ->addValue('entity_id', $contact2['id'])
67 ->addValue('entity_table', 'civicrm_contact')
68 ->addValue('tag_id', $setChild['id'])
69 ->execute();
70
71 $shouldReturnContact1 = Contact::get(FALSE)
72 ->addSelect('id')
73 ->addWhere('tags:name', 'IN', [$conTag['name']])
74 ->execute();
75 $this->assertCount(1, $shouldReturnContact1);
76 $this->assertEquals($contact1['id'], $shouldReturnContact1->first()['id']);
77
78 $shouldReturnContact2 = Contact::get(FALSE)
79 ->addSelect('id')
80 ->addWhere('tags', 'IN', [$setChild['id']])
81 ->execute();
82 $this->assertCount(1, $shouldReturnContact2);
83 $this->assertEquals($contact2['id'], $shouldReturnContact2->first()['id']);
84 }
85
86 }