3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
38 * Generic function to compare expected values after an api call to retrieved
41 * @daoName string DAO Name of object we're evaluating.
42 * @id int Id of object
43 * @match array Associative array of field name => expected value. Empty if asserting
44 * that a DELETE occurred
45 * @delete boolean True if we're checking that a DELETE action occurred.
52 public function assertDBState(&$testCase, $daoName, $id, $match, $delete = FALSE) {
54 // adding this here since developers forget to check for an id
55 // and hence we get the first value in the db
56 $testCase->fail('ID not populated. Please fix your assertDBState usage!!!');
59 $object = new $daoName();
63 // If we're asserting successful record deletion, make sure object is NOT found.
65 if ($object->find(TRUE)) {
66 $testCase->fail("Object not deleted by delete operation: $daoName, $id");
71 // Otherwise check matches of DAO field values against expected values in $match.
72 if ($object->find(TRUE)) {
73 $fields = &$object->fields();
74 foreach ($fields as $name => $value) {
75 $dbName = $value['name'];
76 if (isset($match[$name])) {
78 $testCase->assertEquals($object->$dbName, $match[$name]);
80 elseif (isset($match[$dbName])) {
82 $testCase->assertEquals($object->$dbName, $match[$dbName]);
87 $testCase->fail("Could not retrieve object: $daoName, $id");
89 $matchSize = count($match);
90 if ($verifiedCount != $matchSize) {
91 $testCase->fail("Did not verify all fields in match array: $daoName, $id. Verified count = $verifiedCount. Match array size = $matchSize");
96 * Request a record from the DB by seachColumn+searchValue. Success if a record is found.
100 * @param $returnColumn
101 * @param $searchColumn
103 * @return null|string
105 public function assertDBNotNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) {
106 if (empty($searchValue)) {
107 $testCase->fail("empty value passed to assertDBNotNull");
109 $value = CRM_Core_DAO
::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn);
110 $testCase->assertNotNull($value, $message);
116 * Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL.
119 * @param $searchValue
120 * @param $returnColumn
121 * @param $searchColumn
124 public function assertDBNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) {
125 $value = CRM_Core_DAO
::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn);
126 $testCase->assertNull($value, $message);
130 * Request a record from the DB by id. Success if row not found.
136 public function assertDBRowNotExist(&$testCase, $daoName, $id, $message) {
137 $value = CRM_Core_DAO
::getFieldValue($daoName, $id, 'id', 'id');
138 $testCase->assertNull($value, $message);
142 * Compare a single column value in a retrieved DB record to an expected value.
145 * @param string $daoName
146 * @param $searchValue
147 * @param $returnColumn
148 * @param $searchColumn
149 * @param $expectedValue
150 * @param string $message
152 public function assertDBCompareValue(
153 &$testCase, $daoName, $searchValue, $returnColumn, $searchColumn,
154 $expectedValue, $message
156 $value = CRM_Core_DAO
::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn);
157 $testCase->assertEquals($value, $expectedValue, $message);
161 * Compare all values in a single retrieved DB record to an array of expected values.
164 * @param $searchParams
165 * @param $expectedValues
167 public function assertDBCompareValues(&$testCase, $daoName, $searchParams, $expectedValues) {
168 //get the values from db
170 CRM_Core_DAO
::commonRetrieve($daoName, $searchParams, $dbValues);
172 // compare db values with expected values
173 self
::assertAttributesEquals($testCase, $expectedValues, $dbValues);
178 * @param $expectedValues
179 * @param $actualValues
181 public function assertAttributesEquals(&$testCase, &$expectedValues, &$actualValues) {
182 foreach ($expectedValues as $paramName => $paramValue) {
183 if (isset($actualValues[$paramName])) {
184 $testCase->assertEquals($paramValue, $actualValues[$paramName]);
187 $testCase->fail("Attribute '$paramName' not present in actual array.");