APIv4 - Fix resolving pseudoconstants for less-permissioned users
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / TagTest.php
CommitLineData
a6d0f90f
CW
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
19namespace api\v4\Entity;
20
21use Civi\Api4\Contact;
46f571dd 22use api\v4\Api4TestBase;
a6d0f90f
CW
23use Civi\Api4\EntityTag;
24use Civi\Api4\Tag;
25use Civi\Test\TransactionalInterface;
26
27/**
28 * @group headless
29 */
46f571dd 30class TagTest extends Api4TestBase implements TransactionalInterface {
a6d0f90f
CW
31
32 public function testTagFilter() {
05d4c0d2
CW
33 // Ensure bypassing permissions works correctly by giving none to the logged-in user
34 $this->createLoggedInUser();
35 \CRM_Core_Config::singleton()->userPermissionClass->permissions = [];
36
a6d0f90f
CW
37 $conTag = Tag::create(FALSE)
38 ->addValue('name', uniqid('con'))
39 ->addValue('used_for', 'civicrm_contact')
40 ->addValue('color', '#cccccc')
41 ->execute()->first();
42 $tagChild = Tag::create(FALSE)
43 ->addValue('name', uniqid('child'))
44 ->addValue('parent_id', $conTag['id'])
45 ->execute()->first();
46 $tagSubChild = Tag::create(FALSE)
47 ->addValue('name', uniqid('child'))
48 ->addValue('parent_id', $tagChild['id'])
49 ->execute()->first();
50 $tagSet = Tag::create(FALSE)
51 ->addValue('name', uniqid('set'))
52 ->addValue('used_for', 'civicrm_contact')
53 ->addValue('is_tagset', TRUE)
54 ->execute()->first();
55 $setChild = Tag::create(FALSE)
56 ->addValue('name', uniqid('child'))
57 ->addValue('parent_id', $tagSet['id'])
58 ->execute()->first();
59
60 $contact1 = Contact::create(FALSE)
61 ->execute()->first();
62 $contact2 = Contact::create(FALSE)
63 ->execute()->first();
64 EntityTag::create(FALSE)
65 ->addValue('entity_id', $contact1['id'])
66 ->addValue('entity_table', 'civicrm_contact')
67 ->addValue('tag_id', $tagSubChild['id'])
68 ->execute();
69 EntityTag::create(FALSE)
70 ->addValue('entity_id', $contact2['id'])
71 ->addValue('entity_table', 'civicrm_contact')
72 ->addValue('tag_id', $setChild['id'])
73 ->execute();
74
75 $shouldReturnContact1 = Contact::get(FALSE)
76 ->addSelect('id')
77 ->addWhere('tags:name', 'IN', [$conTag['name']])
78 ->execute();
79 $this->assertCount(1, $shouldReturnContact1);
80 $this->assertEquals($contact1['id'], $shouldReturnContact1->first()['id']);
81
82 $shouldReturnContact2 = Contact::get(FALSE)
83 ->addSelect('id')
84 ->addWhere('tags', 'IN', [$setChild['id']])
85 ->execute();
86 $this->assertCount(1, $shouldReturnContact2);
87 $this->assertEquals($contact2['id'], $shouldReturnContact2->first()['id']);
88 }
89
90}