Commit | Line | Data |
---|---|---|
dc86f881 CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06a1bc01 | 4 | | CiviCRM version 4.5 | |
dc86f881 | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
dc86f881 CW |
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 | */ | |
27 | ||
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | ||
30 | /** | |
2158332a | 31 | * Tests for field options |
dc86f881 CW |
32 | */ |
33 | class CRM_Core_FieldOptionsTest extends CiviUnitTestCase { | |
4cbe18b8 EM |
34 | /** |
35 | * @return array | |
36 | */ | |
dc86f881 CW |
37 | function get_info() { |
38 | return array( | |
262f0ecc | 39 | 'name' => 'FieldOptions', |
dc86f881 | 40 | 'description' => 'Tests for field-specific option values', |
262f0ecc | 41 | 'group' => 'Core', |
dc86f881 CW |
42 | ); |
43 | } | |
44 | ||
45 | function setUp() { | |
46 | parent::setUp(); | |
47 | } | |
48 | ||
49 | /** | |
50 | * Assure CRM_Core_PseudoConstant::get() is working properly for a range of | |
51 | * DAO fields having a <pseudoconstant> tag in the XML schema. | |
52 | */ | |
53 | function testOptionValues() { | |
54 | /** | |
55 | * baoName/field combinations to test | |
56 | * Format: array[BAO Name] = $properties, where properties is an array whose | |
57 | * named members can be: | |
58 | * - fieldName: the SQL column name within the DAO table. | |
59 | * - sample: Any one value which is expected in the list of option values. | |
60 | * - context: Context to pass | |
61 | * - props: Object properties to pass | |
62 | * - exclude: Any one value which should not be in the list. | |
63 | * - max: integer (default = 10) maximum number of option values expected. | |
64 | */ | |
65 | $fields = array( | |
66 | 'CRM_Core_BAO_Address' => array( | |
67 | array( | |
68 | 'fieldName' => 'state_province_id', | |
69 | 'sample' => 'California', | |
70 | 'max' => 60, | |
71 | 'props' => array('country_id' => 1228), | |
72 | ), | |
73 | ), | |
74 | 'CRM_Contact_BAO_Contact' => array( | |
75 | array( | |
76 | 'fieldName' => 'contact_sub_type', | |
77 | 'sample' => 'Team', | |
78 | 'exclude' => 'Organization', | |
79 | 'props' => array('contact_type' => 'Organization'), | |
80 | ), | |
81 | ), | |
82 | ); | |
83 | ||
84 | foreach ($fields as $baoName => $baoFields) { | |
85 | foreach ($baoFields as $field) { | |
86 | $message = "BAO name: '{$baoName}', field: '{$field['fieldName']}'"; | |
87 | ||
88 | $props = CRM_Utils_Array::value('props', $field, array()); | |
262f0ecc | 89 | $optionValues = $baoName::buildOptions($field['fieldName'], 'create', $props); |
dc86f881 CW |
90 | $this->assertNotEmpty($optionValues, $message); |
91 | ||
92 | // Ensure sample value is contained in the returned optionValues. | |
93 | $this->assertContains($field['sample'], $optionValues, $message); | |
94 | ||
95 | // Exclude test | |
96 | if (!empty($field['exclude'])) { | |
97 | $this->assertNotContains($field['exclude'], $optionValues, $message); | |
98 | } | |
99 | ||
100 | // Ensure count of optionValues is not extraordinarily high. | |
101 | $max = CRM_Utils_Array::value('max', $field, 10); | |
102 | $this->assertLessThanOrEqual($max, count($optionValues), $message); | |
103 | } | |
104 | } | |
105 | } | |
106 | } |