| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group taxonomy |
| | 5 | */ |
| | 6 | class Tests_Term_termCount extends WP_UnitTestCase { |
| | 7 | |
| | 8 | /** |
| | 9 | * Term ID for testing attachment counts. |
| | 10 | * |
| | 11 | * @var int |
| | 12 | */ |
| | 13 | public static $attachment_term; |
| | 14 | |
| | 15 | public static function wpSetUpBeforeClass( $factory ) { |
| | 16 | register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) ); |
| | 17 | self::$attachment_term = $factory->term->create( array( 'taxonomy' => 'wp_test_tax_counts' ) ); |
| | 18 | } |
| | 19 | |
| | 20 | public function setUp() { |
| | 21 | parent::setUp(); |
| | 22 | |
| | 23 | register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) ); |
| | 24 | } |
| | 25 | |
| | 26 | /** |
| | 27 | * Term counts increments correctly when post status becomes published. |
| | 28 | * |
| | 29 | * @dataProvider data_term_counts_incremented_on_publish |
| | 30 | * @ticket 40351 |
| | 31 | * @ticket 51292 |
| | 32 | * |
| | 33 | * @param string $original_post_status Post status prior to change to publish. |
| | 34 | * @param int $change Expected change upon publish. |
| | 35 | */ |
| | 36 | public function test_term_counts_incremented_on_publish( $original_post_status, $change ) { |
| | 37 | $post_id = self::factory()->post->create( array( 'post_status' => $original_post_status ) ); |
| | 38 | $term_count = get_term( get_option( 'default_category' ) )->count; |
| | 39 | |
| | 40 | wp_publish_post( $post_id ); |
| | 41 | |
| | 42 | $expected = $term_count + $change; |
| | 43 | $this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count ); |
| | 44 | } |
| | 45 | |
| | 46 | /** |
| | 47 | * Data provider for test_term_count_changes_for_post_statuses. |
| | 48 | * |
| | 49 | * @return array[] { |
| | 50 | * @type string $original_post_status Post status prior to change to publish. |
| | 51 | * @type int $change Expected change upon publish. |
| | 52 | * } |
| | 53 | */ |
| | 54 | function data_term_counts_incremented_on_publish() { |
| | 55 | return array( |
| | 56 | // 0. Published post |
| | 57 | array( 'publish', 0 ), |
| | 58 | // 1. Auto draft |
| | 59 | array( 'auto-draft', 1 ), |
| | 60 | // 2. Draft |
| | 61 | array( 'draft', 1 ), |
| | 62 | // 3. Private post |
| | 63 | array( 'private', 1 ), |
| | 64 | ); |
| | 65 | } |
| | 66 | |
| | 67 | /** |
| | 68 | * Term counts increments correctly when post status becomes published. |
| | 69 | * |
| | 70 | * @dataProvider data_term_counts_incremented_on_publish_with_attachments |
| | 71 | * @ticket 40351 |
| | 72 | * @ticket 51292 |
| | 73 | * |
| | 74 | * @param string $original_post_status Post status prior to change to publish. |
| | 75 | * @param int $change Expected change upon publish. |
| | 76 | */ |
| | 77 | public function test_term_counts_incremented_on_publish_with_attachments( $original_post_status, $change ) { |
| | 78 | $post_id = self::factory()->post->create( array( 'post_status' => $original_post_status ) ); |
| | 79 | wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' ); |
| | 80 | $attachment_id = self::factory()->attachment->create_object( |
| | 81 | array( |
| | 82 | 'file' => 'image.jpg', |
| | 83 | 'post_parent' => $post_id, |
| | 84 | 'post_status' => 'inherit', |
| | 85 | ) |
| | 86 | ); |
| | 87 | wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' ); |
| | 88 | $term_count = get_term( self::$attachment_term )->count; |
| | 89 | |
| | 90 | wp_publish_post( $post_id ); |
| | 91 | |
| | 92 | $expected = $term_count + $change; |
| | 93 | $this->assertSame( $expected, get_term( self::$attachment_term )->count ); |
| | 94 | } |
| | 95 | |
| | 96 | /** |
| | 97 | * Data provider for test_term_count_changes_for_post_statuses_with_attachments. |
| | 98 | * |
| | 99 | * @return array[] { |
| | 100 | * @type string $original_post_status Post status prior to change to publish. |
| | 101 | * @type int $change Expected change upon publish. |
| | 102 | * } |
| | 103 | */ |
| | 104 | function data_term_counts_incremented_on_publish_with_attachments() { |
| | 105 | return array( |
| | 106 | // 0. Published post |
| | 107 | array( 'publish', 0 ), |
| | 108 | // 1. Auto draft |
| | 109 | array( 'auto-draft', 2 ), |
| | 110 | // 2. Draft |
| | 111 | array( 'draft', 2 ), |
| | 112 | // 3. Private post |
| | 113 | array( 'private', 2 ), |
| | 114 | ); |
| | 115 | } |
| | 116 | |
| | 117 | /** |
| | 118 | * Term counts increments correctly when post status becomes published. |
| | 119 | * |
| | 120 | * @dataProvider data_term_counts_incremented_on_publish_with_untermed_attachments |
| | 121 | * @ticket 40351 |
| | 122 | * @ticket 51292 |
| | 123 | * |
| | 124 | * @param string $original_post_status Post status prior to change to publish. |
| | 125 | * @param int $change Expected change upon publish. |
| | 126 | */ |
| | 127 | public function test_term_counts_incremented_on_publish_with_untermed_attachments( $original_post_status, $change ) { |
| | 128 | $post_id = self::factory()->post->create( array( 'post_status' => $original_post_status ) ); |
| | 129 | wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' ); |
| | 130 | $attachment_id = self::factory()->attachment->create_object( |
| | 131 | array( |
| | 132 | 'file' => 'image.jpg', |
| | 133 | 'post_parent' => $post_id, |
| | 134 | 'post_status' => 'inherit', |
| | 135 | ) |
| | 136 | ); |
| | 137 | $term_count = get_term( self::$attachment_term )->count; |
| | 138 | |
| | 139 | wp_publish_post( $post_id ); |
| | 140 | |
| | 141 | $expected = $term_count + $change; |
| | 142 | $this->assertSame( $expected, get_term( self::$attachment_term )->count ); |
| | 143 | } |
| | 144 | |
| | 145 | /** |
| | 146 | * Data provider for test_term_count_changes_for_post_statuses_with_untermed_attachments. |
| | 147 | * |
| | 148 | * @return array[] { |
| | 149 | * @type string $original_post_status Post status prior to change to publish. |
| | 150 | * @type int $change Expected change upon publish. |
| | 151 | * } |
| | 152 | */ |
| | 153 | function data_term_counts_incremented_on_publish_with_untermed_attachments() { |
| | 154 | return array( |
| | 155 | // 0. Published post |
| | 156 | array( 'publish', 0 ), |
| | 157 | // 1. Auto draft |
| | 158 | array( 'auto-draft', 1 ), |
| | 159 | // 2. Draft |
| | 160 | array( 'draft', 1 ), |
| | 161 | // 3. Private post |
| | 162 | array( 'private', 1 ), |
| | 163 | ); |
| | 164 | } |
| | 165 | } |