Merge pull request #17203 from artfulrobot/artfulrobot-cleanup-job-improvements
[civicrm-core.git] / tests / phpunit / api / v4 / Utils / SelectUtilTest.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 * $Id$
18 *
19 */
20
21
22 namespace api\v4\Utils;
23
24 use api\v4\UnitTestCase;
25 use Civi\Api4\Utils\SelectUtil;
26
27 /**
28 * @group headless
29 */
30 class SelectUtilTest extends UnitTestCase {
31
32 private $emailFieldNames = [
33 'id',
34 'contact_id',
35 'location_type_id',
36 'email',
37 'is_primary',
38 'is_billing',
39 'on_hold',
40 'is_bulkmail',
41 'hold_date',
42 'reset_date',
43 'signature_text',
44 'signature_html',
45 'contact.id',
46 'contact.display_name',
47 'contact.sort_name',
48 'contact.phone.id',
49 'contact.phone.phone',
50 'contact.phone.phone_type_id',
51 ];
52
53 public function getSelectExamples() {
54 return [
55 ['any', ['*'], TRUE],
56 ['any', ['*', 'one', 'two'], TRUE],
57 ['one', ['one', 'two'], TRUE],
58 ['one', ['o*', 'two'], TRUE],
59 ['one', ['*o', 'two'], FALSE],
60 ['zoo', ['one', 'two'], FALSE],
61 ['one.id', ['one.id', 'two'], TRUE],
62 ['one.id', ['one.*', 'two'], TRUE],
63 ];
64 }
65
66 /**
67 * @dataProvider getSelectExamples
68 * @param string $field
69 * @param array $selects
70 * @param bool $expected
71 */
72 public function testIsFieldSelected($field, $selects, $expected) {
73 $this->assertEquals($expected, SelectUtil::isFieldSelected($field, $selects));
74 }
75
76 public function getMatchingExamples() {
77 return [
78 [array_slice($this->emailFieldNames, 0, 12), '*'],
79 [[], 'nothing'],
80 [['email'], 'email'],
81 [['contact_id', 'location_type_id'], '*_id'],
82 [['contact_id', 'location_type_id'], '*o*_id'],
83 [['contact_id'], 'con*_id'],
84 [['is_primary', 'is_billing', 'is_bulkmail'], 'is_*'],
85 [['is_billing', 'is_bulkmail'], 'is_*l*'],
86 [['contact.id', 'contact.display_name', 'contact.sort_name'], 'contact.*'],
87 [['contact.display_name', 'contact.sort_name'], 'contact.*_name'],
88 [['contact.phone.id', 'contact.phone.phone', 'contact.phone.phone_type_id'], 'contact.phone.*'],
89 [['contact.phone.phone', 'contact.phone.phone_type_id'], 'contact.phone.phone*'],
90 ];
91 }
92
93 /**
94 * @dataProvider getMatchingExamples
95 * @param $expected
96 * @param $pattern
97 */
98 public function testGetMatchingFields($expected, $pattern) {
99 $this->assertEquals($expected, SelectUtil::getMatchingFields($pattern, $this->emailFieldNames));
100 }
101
102 }