-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest-subscription-attempts.php
88 lines (76 loc) · 3.2 KB
/
test-subscription-attempts.php
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
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* Class Newsletters Test Subscription_Attempts
*
* @package Newspack_Newsletters
*/
use Newspack\Newsletters\Subscription_Attempts;
/**
* Tests the Subscription_Attempts class
*/
class Subscription_Attempts_Test extends WP_UnitTestCase {
/**
* Test DB version.
*/
public static function test_subscription_attempts_db_version() {
$version = get_option( Newspack_Newsletters_Subscription_Attempts::TABLE_VERSION_OPTION );
self::assertEquals( Newspack_Newsletters_Subscription_Attempts::TABLE_VERSION, $version );
}
/**
* Set up
*/
public function set_up() {
global $wpdb;
$table_name = Newspack_Newsletters_Subscription_Attempts::get_table_name();
$wpdb->query( "DELETE FROM $table_name" ); // phpcs:ignore
}
/**
* Test if the attempt is added to the custom table when the WP hook is called.
*/
public function test_subscription_attempts_add_and_update() {
$lists = [ 'list1', 'list2' ];
$contact = [
'email' => '[email protected]',
];
do_action( 'newspack_newsletters_pre_add_contact', $lists, $contact );
$result = Newspack_Newsletters_Subscription_Attempts::get_by_email( $contact['email'] );
self::assertEquals( $contact['email'], $result->email );
self::assertEquals( implode( ',', $lists ), $result->list_ids );
$lists_added = [ 'list3', 'list4' ];
do_action( 'newspack_newsletters_update_contact_lists', 'some_esp', $contact['email'], $lists_added, [], true, 'test' );
$result = Newspack_Newsletters_Subscription_Attempts::get_by_email( $contact['email'] );
$lists_expected = array_merge( $lists, $lists_added );
self::assertEquals( implode( ',', $lists_expected ), $result->list_ids );
$lists_removed = [ 'list1', 'list3' ];
do_action( 'newspack_newsletters_update_contact_lists', 'some_esp', $contact['email'], [], $lists_removed, true, 'test' );
$result = Newspack_Newsletters_Subscription_Attempts::get_by_email( $contact['email'] );
$lists_expected = [ 'list2', 'list4' ];
self::assertEquals( implode( ',', $lists_expected ), $result->list_ids );
}
/**
* Test data cleanup.
*/
public function test_subscription_attempts_cleanup() {
global $wpdb;
$table_name = Newspack_Newsletters_Subscription_Attempts::get_table_name();
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$table_name,
[
'email' => '[email protected]',
'list_ids' => '1,2',
// Set created time to one year ago.
'created_at' => \strtotime( '-1 year' ),
],
[
'%s',
'%s',
'%s',
]
);
$all_entries = $wpdb->get_results( "SELECT * FROM $table_name", ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
self::assertEquals( 1, count( $all_entries ) );
Newspack_Newsletters_Subscription_Attempts::cleanup();
$all_entries = $wpdb->get_results( "SELECT * FROM $table_name", ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
self::assertEmpty( $all_entries );
}
}