Update copyright date for 2020
[civicrm-core.git] / tests / phpunit / CRM / Custom / Page / AJAXTest.php
CommitLineData
e525d6af 1<?php
cf0a66ac 2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
cf0a66ac 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
cf0a66ac 7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
e525d6af 27
28/**
29 * Class CRM_Custom_Page_AJAXTest
30 * @group headless
31 */
32class CRM_Custom_Page_AJAXTest extends CiviUnitTestCase {
33
34 /**
35 * Set up function.
36 */
37 public function setUp() {
38 parent::setUp();
39 }
40
41 /**
42 * Test multi-record custom fields
43 */
44 public function testMultiRecordFieldList() {
45 //create multi record custom group
9099cab3
CW
46 $ids = $this->CustomGroupMultipleCreateWithFields(['style' => 'Tab with table']);
47 $params = [
e525d6af 48 'contact_type' => 'Individual',
49 'first_name' => 'Test',
50 'last_name' => 'Contact',
9099cab3 51 ];
e525d6af 52 $customFields = $ids['custom_field_id'];
53 $result = $this->callAPISuccess('contact', 'create', $params);
54 $contactId = $result['id'];
55
56 //enter values for custom fields
9099cab3 57 $customParams = [
e525d6af 58 "custom_{$customFields[0]}_-1" => "test value {$customFields[0]} one",
59 "custom_{$customFields[0]}_-2" => "test value {$customFields[0]} two",
aa4b06ee 60 "custom_{$customFields[0]}_-3" => "test value {$customFields[0]} three",
e525d6af 61 "custom_{$customFields[1]}_-1" => "test value {$customFields[1]} one",
62 "custom_{$customFields[1]}_-2" => "test value {$customFields[1]} two",
aa4b06ee 63 "custom_{$customFields[1]}_-3" => "test value {$customFields[1]} three",
e525d6af 64 "custom_{$customFields[2]}_-1" => "test value {$customFields[2]} one",
65 "custom_{$customFields[2]}_-2" => "test value {$customFields[2]} two",
aa4b06ee 66 "custom_{$customFields[2]}_-3" => "test value {$customFields[2]} three",
9099cab3 67 ];
e525d6af 68 CRM_Core_BAO_CustomValueTable::postProcess($customParams, "civicrm_contact", $contactId, NULL);
69
9099cab3 70 $_GET = [
e525d6af 71 'cid' => $contactId,
72 'cgid' => $ids['custom_group_id'],
73 'is_unit_test' => TRUE,
9099cab3 74 ];
e525d6af 75 $multiRecordFields = CRM_Custom_Page_AJAX::getMultiRecordFieldList();
76
77 //check sorting
78 foreach ($customFields as $fieldId) {
79 $columnName = "field_{$fieldId}{$ids['custom_group_id']}_{$fieldId}";
9099cab3 80 $_GET['columns'][] = [
e525d6af 81 'data' => $columnName,
9099cab3 82 ];
e525d6af 83 }
84 // get the results in descending order
9099cab3
CW
85 $_GET['order'] = [
86 '0' => [
e525d6af 87 'column' => 0,
88 'dir' => 'desc',
9099cab3
CW
89 ],
90 ];
e525d6af 91 $sortedRecords = CRM_Custom_Page_AJAX::getMultiRecordFieldList();
92
aa4b06ee 93 $this->assertEquals(3, $sortedRecords['recordsTotal']);
94 $this->assertEquals(3, $multiRecordFields['recordsTotal']);
e525d6af 95 foreach ($customFields as $fieldId) {
96 $columnName = "field_{$fieldId}{$ids['custom_group_id']}_{$fieldId}";
97 $this->assertEquals("test value {$fieldId} one", $multiRecordFields['data'][0][$columnName]['data']);
98 $this->assertEquals("test value {$fieldId} two", $multiRecordFields['data'][1][$columnName]['data']);
aa4b06ee 99 $this->assertEquals("test value {$fieldId} three", $multiRecordFields['data'][2][$columnName]['data']);
e525d6af 100
101 // this should be sorted in descending order.
102 $this->assertEquals("test value {$fieldId} two", $sortedRecords['data'][0][$columnName]['data']);
aa4b06ee 103 $this->assertEquals("test value {$fieldId} three", $sortedRecords['data'][1][$columnName]['data']);
104 $this->assertEquals("test value {$fieldId} one", $sortedRecords['data'][2][$columnName]['data']);
e525d6af 105 }
106
e525d6af 107 $sorted = FALSE;
aa4b06ee 108 // sorted order result should be two, three, one
9099cab3
CW
109 $sortedCount = [1 => 2, 2 => 3, 3 => 1];
110 foreach ([$multiRecordFields, $sortedRecords] as $records) {
e525d6af 111 $count = 1;
e525d6af 112 foreach ($records['data'] as $key => $val) {
aa4b06ee 113 //check links for result sorted in descending order
114 if ($sorted) {
115 $initialCount = $count;
116 $count = $sortedCount[$count];
117 }
118 // extract view, edit, copy links and assert the recId, cgcount.
e525d6af 119 preg_match_all('!https?://\S+!', $val['action'], $matches);
120 foreach ($matches[0] as $match) {
121 $parts = parse_url($match);
122 $parts['query'] = str_replace('&amp;', '&', $parts['query']);
123 parse_str($parts['query'], $query);
124
125 switch (trim($query['mode'], '"')) {
126 case 'view':
127 $this->assertEquals($count, $query['recId']);
128 break;
129
130 case 'edit':
131 $this->assertEquals($count, $query['cgcount']);
132 break;
133
134 case 'copy':
aa4b06ee 135 $this->assertEquals(4, $query['cgcount']);
e525d6af 136 break;
137 }
138 }
aa4b06ee 139 if (!empty($initialCount)) {
140 $count = $initialCount;
e525d6af 141 }
aa4b06ee 142
143 $count++;
e525d6af 144 }
145 $sorted = TRUE;
146 }
147 }
148
149}