|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test file for Activitypub Follower. |
| 4 | + * |
| 5 | + * @package Activitypub |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Activitypub\Tests\Model; |
| 9 | + |
| 10 | +use Activitypub\Model\Follower; |
| 11 | +use Activitypub\Collection\Followers; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests the Follower class. |
| 15 | + * |
| 16 | + * @package Activitypub |
| 17 | + */ |
| 18 | +class Test_Follower extends \WP_UnitTestCase { |
| 19 | + /** |
| 20 | + * Tests clear_errors. |
| 21 | + * |
| 22 | + * @covers ::clear_errors |
| 23 | + */ |
| 24 | + public function test_clear_errors() { |
| 25 | + // Mock request. |
| 26 | + $follower = new Follower(); |
| 27 | + $follower->from_array( |
| 28 | + array( |
| 29 | + 'id' => 'https://example.com/author/jon', |
| 30 | + 'type' => 'Person', |
| 31 | + 'name' => 'Jon Doe', |
| 32 | + 'preferredUsername' => 'jon', |
| 33 | + 'inbox' => 'https://example.com/author/jon/inbox', |
| 34 | + 'publicKey' => 'publicKey', |
| 35 | + 'publicKeyPem' => 'publicKeyPem', |
| 36 | + ) |
| 37 | + ); |
| 38 | + |
| 39 | + $id = $follower->upsert(); |
| 40 | + $this->assertNotWPError( $id ); |
| 41 | + |
| 42 | + // Add some errors. |
| 43 | + Followers::add_error( $follower->get__id(), 'Test error 1' ); |
| 44 | + Followers::add_error( $follower->get__id(), 'Test error 2' ); |
| 45 | + |
| 46 | + // Verify errors were added. |
| 47 | + $errors = $follower->get_errors(); |
| 48 | + $this->assertCount( 2, $errors ); |
| 49 | + |
| 50 | + // Clear errors. |
| 51 | + $cleared = $follower->clear_errors(); |
| 52 | + $this->assertTrue( $cleared ); |
| 53 | + |
| 54 | + // Verify errors were cleared. |
| 55 | + $errors = $follower->get_errors(); |
| 56 | + $this->assertEmpty( $errors ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Tests clear_errors with no errors. |
| 61 | + * |
| 62 | + * @covers ::clear_errors |
| 63 | + */ |
| 64 | + public function test_clear_errors_no_errors() { |
| 65 | + $follower = new Follower(); |
| 66 | + $follower->from_array( |
| 67 | + array( |
| 68 | + 'id' => 'https://example.com/author/jon', |
| 69 | + 'type' => 'Person', |
| 70 | + 'name' => 'Jon Doe', |
| 71 | + 'preferredUsername' => 'jon', |
| 72 | + 'inbox' => 'https://example.com/author/jon/inbox', |
| 73 | + 'publicKey' => 'publicKey', |
| 74 | + 'publicKeyPem' => 'publicKeyPem', |
| 75 | + ) |
| 76 | + ); |
| 77 | + $id = $follower->upsert(); |
| 78 | + $this->assertNotWPError( $id ); |
| 79 | + |
| 80 | + // Clear errors when none exist. |
| 81 | + $cleared = $follower->clear_errors(); |
| 82 | + $this->assertFalse( $cleared ); |
| 83 | + |
| 84 | + // Verify no errors exist. |
| 85 | + $errors = $follower->get_errors(); |
| 86 | + $this->assertEmpty( $errors ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Tests clear_errors triggers _doing_it_wrong when ID is not set. |
| 91 | + * |
| 92 | + * @covers ::clear_errors |
| 93 | + */ |
| 94 | + public function test_clear_errors_doing_it_wrong() { |
| 95 | + $this->setExpectedIncorrectUsage( 'Activitypub\Model\Follower::clear_errors' ); |
| 96 | + |
| 97 | + $follower = new Follower(); |
| 98 | + $follower->clear_errors(); |
| 99 | + } |
| 100 | +} |
0 commit comments