-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathInsertRuleDataTest.php
More file actions
77 lines (64 loc) · 2.64 KB
/
Copy pathInsertRuleDataTest.php
File metadata and controls
77 lines (64 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Test file for InsertRuleData
*
* @package Accessibility_Checker
*/
use EDAC\Admin\Insert_Rule_Data;
/**
* Test class for InsertRuleData
*/
class InsertRuleDataTest extends WP_UnitTestCase {
/**
* Create table to test against.
*
* @return void
*/
public function setUp(): void {
global $wpdb;
$this->table_name = $wpdb->prefix . 'accessibility_checker';
// Use the Update_Database class to create/update the table schema.
require_once dirname( __DIR__, 3 ) . '/admin/class-update-database.php';
$update_db = new \EDAC\Admin\Update_Database();
$update_db->edac_update_database();
}
/**
* Cleans up the table after each test.
*
* @return void
*/
public function tearDown(): void {
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS $this->table_name" ); // phpcs:ignore WordPress.DB -- Table name is safe and not caching in a test.
}
/**
* Tests the insert method would return expected data types.
*/
public function testRuleInserterReturnLogic() {
$post = $this->factory()->post->create_and_get();
$rule = 'rule';
$ruletype = 'ruletype';
$rule_obj = 'rule_obj';
global $wpdb;
$rule_inserter = new Insert_Rule_Data();
$initial_row_count = $wpdb->get_var( "SELECT COUNT(*) FROM $this->table_name" ); // phpcs:ignore WordPress.DB -- caching not required for one time operation.
// call should return int as a successful insert.
$new_data = $rule_inserter->insert( $post, $rule, $ruletype, $rule_obj );
$this->assertIsInt( $new_data );
// second call is a duplicate and should return null.
$duplicate_data = $rule_inserter->insert( $post, $rule, $ruletype, $rule_obj );
$this->assertEquals( null, $duplicate_data );
// check if the row count has increased by 1.
$current_row_count = $wpdb->get_var( "SELECT COUNT(*) FROM $this->table_name" ); // phpcs:ignore WordPress.DB -- caching not required for one time operation.
$this->assertEquals( $initial_row_count + 1, $current_row_count );
// should return null as ruletype is 'revision'.
$revision_type_return = $rule_inserter->insert( $post, $rule, 'revision', $rule_obj );
$this->assertEquals( null, $revision_type_return );
// should throw an exception because of missing parameters.
$this->expectException( TypeError::class );
$rule_inserter->insert(); // phpcs:ignore -- intentionally passing something that will cause an exception.
// check that row count has not increased since last check.
$current_row_count = $wpdb->get_var( "SELECT COUNT(*) FROM $this->table_name" ); // phpcs:ignore WordPress.DB -- caching not required for one time operation.
$this->assertEquals( $initial_row_count + 1, $current_row_count );
}
}