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 | ||
46f571dd | 22 | use api\v4\Api4TestBase; |
19b53e5b | 23 | use Civi\Api4\Contact; |
46f571dd | 24 | use Civi\Test\TransactionalInterface; |
19b53e5b C |
25 | |
26 | /** | |
27 | * @group headless | |
28 | */ | |
46f571dd | 29 | class ContactChecksumTest extends Api4TestBase implements TransactionalInterface { |
19b53e5b C |
30 | |
31 | public function testGetChecksum() { | |
fe806431 | 32 | $contact = Contact::create(FALSE) |
19b53e5b C |
33 | ->addValue('first_name', 'Check') |
34 | ->addValue('last_name', 'Sum') | |
35 | ->addChain('cs', Contact::getChecksum()->setContactId('$id')->setTtl(500), 0) | |
36 | ->execute() | |
37 | ->first(); | |
38 | ||
39 | $result = Contact::validateChecksum() | |
40 | ->setContactId($contact['id']) | |
41 | ->setChecksum($contact['cs']['checksum']) | |
42 | ->execute() | |
43 | ->first(); | |
44 | ||
45 | $this->assertTrue($result['valid']); | |
46 | } | |
47 | ||
48 | public function testValidateChecksum() { | |
fe806431 | 49 | $cid = Contact::create(FALSE) |
19b53e5b C |
50 | ->addValue('first_name', 'Checker') |
51 | ->addValue('last_name', 'Sum') | |
52 | ->execute() | |
53 | ->first()['id']; | |
54 | ||
55 | $goodCs = \CRM_Contact_BAO_Contact_Utils::generateChecksum($cid, NULL, 500); | |
56 | $badCs = \CRM_Contact_BAO_Contact_Utils::generateChecksum($cid, strtotime('now - 1 week'), 1); | |
57 | ||
58 | $result1 = Contact::validateChecksum() | |
59 | ->setContactId($cid) | |
60 | ->setChecksum($goodCs) | |
61 | ->execute() | |
62 | ->first(); | |
63 | $this->assertTrue($result1['valid']); | |
64 | ||
65 | $result2 = Contact::validateChecksum() | |
66 | ->setContactId($cid) | |
67 | ->setChecksum($badCs) | |
68 | ->execute() | |
69 | ->first(); | |
70 | $this->assertFalse($result2['valid']); | |
71 | } | |
72 | ||
73 | } |