Merge pull request #14367 from MegaphoneJon/financial-58
[civicrm-core.git] / tests / phpunit / api / v3 / ExceptionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 /**
29 * Test class for Dedupe exceptions.
30 *
31 * @package CiviCRM_APIv3
32 * @group headless
33 */
34 class api_v3_ExceptionTest extends CiviUnitTestCase {
35
36 /**
37 * Sets up the fixture, for example, opens a network connection.
38 *
39 * This method is called before a test is executed.
40 */
41 protected function setUp() {
42 $this->useTransaction(TRUE);
43 parent::setUp();
44 }
45
46 /**
47 * Test that when a dedupe exception is created the pair are saved from the merge cache.
48 */
49 public function testCreatingAnExceptionRemovesFromCachedMergePairs() {
50 $contact1 = $this->individualCreate();
51 $contact2 = $this->individualCreate();
52 $defaultRuleGroupID = $this->callAPISuccess('RuleGroup', 'getvalue', [
53 'contact_type' => 'Individual',
54 'used' => 'Unsupervised',
55 'return' => 'id',
56 'options' => ['limit' => 1],
57 ]);
58 $dupes = $this->callAPISuccess('Dedupe', 'getduplicates', ['rule_group_id' => $defaultRuleGroupID]);
59 $this->assertEquals(1, $dupes['count']);
60 $this->callAPISuccess('Exception', 'create', ['contact_id1' => $contact1, 'contact_id2' => $contact2]);
61 $this->assertEquals(0, CRM_Core_DAO::singleValueQuery('
62 SELECT count(*) FROM civicrm_prevnext_cache
63 WHERE (entity_id1 = ' . $contact1 . ' AND entity_id2 = ' . $contact2 . ')
64 OR (entity_id1 = ' . $contact2 . ' AND entity_id2 = ' . $contact1 . ')'
65 ));
66 $dupes = $this->callAPISuccess('Dedupe', 'getduplicates', ['rule_group_id' => $defaultRuleGroupID]);
67 $this->assertEquals(0, $dupes['count']);
68 }
69
70 /**
71 * Per the ajax code there is an expectation the lower id will be contact 1 - ensure api handles this.
72 *
73 * @throws \Exception
74 */
75 public function testExceptionSavesLowerIDFirst() {
76 $contact1 = $this->individualCreate();
77 $contact2 = $this->individualCreate();
78 $this->callAPISuccess('Exception', 'create', ['contact_id1' => $contact2, 'contact_id2' => $contact1]);
79 $this->callAPISuccessGetSingle('Exception', ['contact_id1' => $contact1, 'contact_id2' => $contact2]);
80 }
81
82 }