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