Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
7d61e75f | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
7d61e75f TO |
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 | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | */ |
18 | ||
19 | ||
19b53e5b C |
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() { | |
fe806431 | 30 | $contact = Contact::create(FALSE) |
19b53e5b C |
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() { | |
fe806431 | 47 | $cid = Contact::create(FALSE) |
19b53e5b C |
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 | } |