Merge pull request #23101 from MegaphoneJon/core-1836-js
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / OptionValueTest.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 api\v4\UnitTestCase;
22 use Civi\Api4\OptionGroup;
23 use Civi\Api4\OptionValue;
24 use Civi\Test\TransactionalInterface;
25
26 /**
27 * @group headless
28 */
29 class OptionValueTest extends UnitTestCase implements TransactionalInterface {
30
31 public function testNullDefault() {
32 OptionGroup::create(FALSE)
33 ->addValue('name', 'myTestGroup')
34 ->addValue('title', 'myTestGroup')
35 ->execute();
36
37 $defaultId = OptionValue::create()
38 ->addValue('option_group_id.name', 'myTestGroup')
39 ->addValue('label', 'One')
40 ->addValue('value', 1)
41 ->addValue('is_default', TRUE)
42 ->execute()->first()['id'];
43
44 $this->assertTrue(OptionValue::get(FALSE)->addWhere('id', '=', $defaultId)->execute()->first()['is_default']);
45
46 // Now create a second option with is_default set to null.
47 // This should not interfere with the default setting in option one
48 OptionValue::create()
49 ->addValue('option_group_id.name', 'myTestGroup')
50 ->addValue('label', 'Two')
51 ->addValue('value', 2)
52 ->addValue('is_default', NULL)
53 ->execute();
54
55 $this->assertTrue(OptionValue::get(FALSE)->addWhere('id', '=', $defaultId)->execute()->first()['is_default']);
56 }
57
58 public function testUpdateWeights() {
59 $getValues = function($groupName) {
60 return OptionValue::get(FALSE)
61 ->addWhere('option_group_id.name', '=', $groupName)
62 ->addOrderBy('weight')
63 ->execute()->indexBy('value')->column('weight');
64 };
65
66 // Create 2 option groups. Control group is to ensure updating one doesn't affect the other
67 foreach (['controlGroup', 'experimentalGroup'] as $groupName) {
68 OptionGroup::create(FALSE)
69 ->addValue('name', $groupName)
70 ->execute();
71 $sampleData = [
72 ['label' => 'One', 'value' => 1],
73 ['label' => 'Two', 'value' => 2],
74 ['label' => 'Three', 'value' => 3],
75 ['label' => 'Four', 'value' => 4],
76 ];
77 OptionValue::save(FALSE)
78 ->setRecords($sampleData)
79 ->addDefault('option_group_id.name', $groupName)
80 ->execute();
81 // Default weights should have been set during create
82 $this->assertEquals([1 => 1, 2 => 2, 3 => 3, 4 => 4], $getValues($groupName));
83 }
84
85 // Move first option to last position
86 OptionValue::update(FALSE)
87 ->addWhere('option_group_id.name', '=', 'experimentalGroup')
88 ->addWhere('value', '=', 1)
89 ->addValue('weight', 4)
90 ->execute();
91 // Experimental group should be updated, control group should not
92 $this->assertEquals([2 => 1, 3 => 2, 4 => 3, 1 => 4], $getValues('experimentalGroup'));
93 $this->assertEquals([1 => 1, 2 => 2, 3 => 3, 4 => 4], $getValues('controlGroup'));
94
95 // Move 2nd (new first) option to last position
96 OptionValue::update(FALSE)
97 ->addWhere('option_group_id.name', '=', 'experimentalGroup')
98 ->addWhere('value', '=', 2)
99 ->addValue('weight', 4)
100 ->execute();
101 // Experimental group should be updated, control group should not
102 $this->assertEquals([3 => 1, 4 => 2, 1 => 3, 2 => 4], $getValues('experimentalGroup'));
103 $this->assertEquals([1 => 1, 2 => 2, 3 => 3, 4 => 4], $getValues('controlGroup'));
104
105 // Move last option to first position
106 OptionValue::update(FALSE)
107 ->addWhere('option_group_id.name', '=', 'experimentalGroup')
108 ->addWhere('value', '=', 2)
109 ->addValue('weight', 1)
110 ->execute();
111 // Experimental group should be updated, control group should not
112 $this->assertEquals([2 => 1, 3 => 2, 4 => 3, 1 => 4], $getValues('experimentalGroup'));
113 $this->assertEquals([1 => 1, 2 => 2, 3 => 3, 4 => 4], $getValues('controlGroup'));
114
115 // Same thing again - should have no impact
116 OptionValue::update(FALSE)
117 ->addWhere('option_group_id.name', '=', 'experimentalGroup')
118 ->addWhere('value', '=', 2)
119 ->addValue('weight', 1)
120 ->execute();
121 // Nothing should have changed
122 $this->assertEquals([2 => 1, 3 => 2, 4 => 3, 1 => 4], $getValues('experimentalGroup'));
123 $this->assertEquals([1 => 1, 2 => 2, 3 => 3, 4 => 4], $getValues('controlGroup'));
124 }
125
126 }