Merge pull request #17920 from eileenmcnaughton/dupe
[civicrm-core.git] / tests / phpunit / api / v4 / Action / ContactChecksumTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20 namespace api\v4\Action;
21
22 use Civi\Api4\Contact;
23
24 /**
25 * @group headless
26 */
27 class ContactChecksumTest extends \api\v4\UnitTestCase {
28
29 public function testGetChecksum() {
30 $contact = Contact::create(FALSE)
31 ->addValue('first_name', 'Check')
32 ->addValue('last_name', 'Sum')
33 ->addChain('cs', Contact::getChecksum()->setContactId('$id')->setTtl(500), 0)
34 ->execute()
35 ->first();
36
37 $result = Contact::validateChecksum()
38 ->setContactId($contact['id'])
39 ->setChecksum($contact['cs']['checksum'])
40 ->execute()
41 ->first();
42
43 $this->assertTrue($result['valid']);
44 }
45
46 public function testValidateChecksum() {
47 $cid = Contact::create(FALSE)
48 ->addValue('first_name', 'Checker')
49 ->addValue('last_name', 'Sum')
50 ->execute()
51 ->first()['id'];
52
53 $goodCs = \CRM_Contact_BAO_Contact_Utils::generateChecksum($cid, NULL, 500);
54 $badCs = \CRM_Contact_BAO_Contact_Utils::generateChecksum($cid, strtotime('now - 1 week'), 1);
55
56 $result1 = Contact::validateChecksum()
57 ->setContactId($cid)
58 ->setChecksum($goodCs)
59 ->execute()
60 ->first();
61 $this->assertTrue($result1['valid']);
62
63 $result2 = Contact::validateChecksum()
64 ->setContactId($cid)
65 ->setChecksum($badCs)
66 ->execute()
67 ->first();
68 $this->assertFalse($result2['valid']);
69 }
70
71 }