|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for PackRelay_Entries_List_Table. |
| 4 | + * |
| 5 | + * @package PackRelay |
| 6 | + * @copyright 2026 MrDemonWolf, Inc. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace PackRelay\Tests; |
| 10 | + |
| 11 | +class EntriesListTableTest extends TestCase { |
| 12 | + |
| 13 | + private \PackRelay_Entries_List_Table $table; |
| 14 | + |
| 15 | + protected function setUp(): void { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + global $wpdb; |
| 19 | + $wpdb = \Mockery::mock(); |
| 20 | + $wpdb->prefix = 'wp_'; |
| 21 | + |
| 22 | + $this->table = new \PackRelay_Entries_List_Table(); |
| 23 | + } |
| 24 | + |
| 25 | + // === column_source === |
| 26 | + |
| 27 | + public function test_column_source_mobile_badge_for_divi(): void { |
| 28 | + $result = $this->table->column_source( array( 'provider' => 'divi' ) ); |
| 29 | + $this->assertStringContainsString( 'packrelay-source-badge mobile', $result ); |
| 30 | + $this->assertStringContainsString( 'Mobile App', $result ); |
| 31 | + } |
| 32 | + |
| 33 | + public function test_column_source_mobile_badge_for_wpforms(): void { |
| 34 | + $result = $this->table->column_source( array( 'provider' => 'wpforms' ) ); |
| 35 | + $this->assertStringContainsString( 'packrelay-source-badge mobile', $result ); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_column_source_mobile_badge_for_gravityforms(): void { |
| 39 | + $result = $this->table->column_source( array( 'provider' => 'gravityforms' ) ); |
| 40 | + $this->assertStringContainsString( 'packrelay-source-badge mobile', $result ); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_column_source_frontend_badge_for_divi_frontend(): void { |
| 44 | + $result = $this->table->column_source( array( 'provider' => 'divi_frontend' ) ); |
| 45 | + $this->assertStringContainsString( 'packrelay-source-badge frontend', $result ); |
| 46 | + $this->assertStringContainsString( 'Divi Frontend', $result ); |
| 47 | + } |
| 48 | + |
| 49 | + public function test_column_source_returns_span_tag(): void { |
| 50 | + $result = $this->table->column_source( array( 'provider' => 'divi' ) ); |
| 51 | + $this->assertStringStartsWith( '<span', $result ); |
| 52 | + $this->assertStringEndsWith( '</span>', $result ); |
| 53 | + } |
| 54 | + |
| 55 | + // === column_provider === |
| 56 | + |
| 57 | + public function test_column_provider_badge_for_divi(): void { |
| 58 | + $result = $this->table->column_provider( array( 'provider' => 'divi' ) ); |
| 59 | + $this->assertStringContainsString( 'packrelay-provider-badge', $result ); |
| 60 | + $this->assertStringContainsString( 'Divi', $result ); |
| 61 | + } |
| 62 | + |
| 63 | + public function test_column_provider_badge_for_divi_frontend(): void { |
| 64 | + $result = $this->table->column_provider( array( 'provider' => 'divi_frontend' ) ); |
| 65 | + $this->assertStringContainsString( 'packrelay-provider-badge', $result ); |
| 66 | + $this->assertStringContainsString( 'Divi', $result ); |
| 67 | + } |
| 68 | + |
| 69 | + public function test_column_provider_badge_for_wpforms(): void { |
| 70 | + $result = $this->table->column_provider( array( 'provider' => 'wpforms' ) ); |
| 71 | + $this->assertStringContainsString( 'packrelay-provider-badge', $result ); |
| 72 | + $this->assertStringContainsString( 'WPForms', $result ); |
| 73 | + } |
| 74 | + |
| 75 | + public function test_column_provider_badge_for_gravityforms(): void { |
| 76 | + $result = $this->table->column_provider( array( 'provider' => 'gravityforms' ) ); |
| 77 | + $this->assertStringContainsString( 'packrelay-provider-badge', $result ); |
| 78 | + $this->assertStringContainsString( 'Gravity Forms', $result ); |
| 79 | + } |
| 80 | + |
| 81 | + public function test_column_provider_falls_back_to_raw_value_for_unknown_provider(): void { |
| 82 | + $result = $this->table->column_provider( array( 'provider' => 'custom_builder' ) ); |
| 83 | + $this->assertStringContainsString( 'packrelay-provider-badge', $result ); |
| 84 | + $this->assertStringContainsString( 'custom_builder', $result ); |
| 85 | + } |
| 86 | + |
| 87 | + // === other columns === |
| 88 | + |
| 89 | + public function test_column_form_id_returns_escaped_value(): void { |
| 90 | + $result = $this->table->column_form_id( array( 'form_id' => '42:0' ) ); |
| 91 | + $this->assertSame( '42:0', $result ); |
| 92 | + } |
| 93 | + |
| 94 | + public function test_column_ip_address_returns_value(): void { |
| 95 | + $result = $this->table->column_ip_address( array( 'ip_address' => '127.0.0.1' ) ); |
| 96 | + $this->assertSame( '127.0.0.1', $result ); |
| 97 | + } |
| 98 | + |
| 99 | + public function test_column_date_created_returns_date_string(): void { |
| 100 | + $result = $this->table->column_date_created( array( 'date_created' => '2026-06-11 12:00:00' ) ); |
| 101 | + $this->assertSame( '2026-06-11 12:00:00', $result ); |
| 102 | + } |
| 103 | + |
| 104 | + public function test_column_fields_returns_mdash_for_invalid_json(): void { |
| 105 | + $result = $this->table->column_fields( array( 'fields' => 'not-json' ) ); |
| 106 | + $this->assertSame( '—', $result ); |
| 107 | + } |
| 108 | + |
| 109 | + public function test_column_fields_returns_mdash_for_non_array_json(): void { |
| 110 | + $result = $this->table->column_fields( array( 'fields' => '"just a string"' ) ); |
| 111 | + $this->assertSame( '—', $result ); |
| 112 | + } |
| 113 | + |
| 114 | + public function test_column_fields_renders_key_value_preview(): void { |
| 115 | + $fields = json_encode( array( 'name' => 'John Doe', 'email' => 'john@example.com' ) ); |
| 116 | + $result = $this->table->column_fields( array( 'fields' => $fields ) ); |
| 117 | + $this->assertStringContainsString( 'name', $result ); |
| 118 | + $this->assertStringContainsString( 'John Doe', $result ); |
| 119 | + $this->assertStringContainsString( 'email', $result ); |
| 120 | + } |
| 121 | + |
| 122 | + public function test_column_fields_limits_preview_to_three_fields(): void { |
| 123 | + $fields = json_encode( array( 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' ) ); |
| 124 | + $result = $this->table->column_fields( array( 'fields' => $fields ) ); |
| 125 | + // At most 3 key: value pairs, each separated by <br>. |
| 126 | + $this->assertLessThanOrEqual( 2, substr_count( $result, '<br>' ) ); |
| 127 | + } |
| 128 | + |
| 129 | + public function test_column_cb_contains_entry_id(): void { |
| 130 | + $result = $this->table->column_cb( array( 'id' => 99 ) ); |
| 131 | + $this->assertStringContainsString( 'value="99"', $result ); |
| 132 | + $this->assertStringContainsString( 'entry_ids[]', $result ); |
| 133 | + } |
| 134 | + |
| 135 | + // === structure === |
| 136 | + |
| 137 | + public function test_get_columns_contains_required_keys(): void { |
| 138 | + $columns = $this->table->get_columns(); |
| 139 | + foreach ( array( 'cb', 'id', 'source', 'provider', 'form_id', 'fields', 'ip_address', 'date_created' ) as $key ) { |
| 140 | + $this->assertArrayHasKey( $key, $columns, "Missing column: $key" ); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public function test_get_sortable_columns_includes_id_and_date(): void { |
| 145 | + $sortable = $this->table->get_sortable_columns(); |
| 146 | + $this->assertArrayHasKey( 'id', $sortable ); |
| 147 | + $this->assertArrayHasKey( 'date_created', $sortable ); |
| 148 | + } |
| 149 | + |
| 150 | + public function test_get_bulk_actions_includes_delete(): void { |
| 151 | + $actions = $this->table->get_bulk_actions(); |
| 152 | + $this->assertArrayHasKey( 'delete', $actions ); |
| 153 | + $this->assertSame( 'Delete', $actions['delete'] ); |
| 154 | + } |
| 155 | +} |
0 commit comments