-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerm_Manager_Test.php
More file actions
155 lines (139 loc) · 3.64 KB
/
Copy pathTerm_Manager_Test.php
File metadata and controls
155 lines (139 loc) · 3.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Test for Term_Manager class.
*
* @package Schedule_Terms
*/
namespace Torounit\WP\Schedule_Terms\Tests;
use Torounit\WP\Schedule_Terms\Term_Manager;
use WP_UnitTestCase;
/**
* Test Class.
*/
class Term_Manager_Test extends WP_UnitTestCase {
/**
* Test for Term_Manager::update_post_term_relations().
*/
public function test_update_post_term_relations() {
$term_manager = new Term_Manager( 'schedule_terms', 'schedule_terms_active', strtotime( '2022-05-29T08:05:41+00:00' ) );
$category_id = $this->factory()->category->create( array( 'name' => 'category-1' ) );
update_term_meta( $category_id, 'schedule_terms_active', true );
$post_id = $this->factory()->post->create();
add_post_meta(
$post_id,
'schedule_terms',
array(
'type' => 'attach',
'taxonomy' => 'category',
'term' => 'category-1',
'datetime' => '2020-07-05T22:09:28+09:00',
),
false
);
add_post_meta(
$post_id,
'schedule_terms',
array(
'type' => 'detach',
'taxonomy' => 'category',
'term' => 'category-1',
'datetime' => '2022-07-05T22:09:28+09:00',
),
false
);
$term_manager->update_post_term_relations( $post_id );
$this->assertTrue( has_category( 'category-1', $post_id ) );
$reflection = new \ReflectionClass( $term_manager );
$time = $reflection->getProperty( 'time' );
$time->setAccessible( true );
$time->setValue( $term_manager, strtotime( '2023-05-29T08:05:41+00:00' ) );
$term_manager->update_post_term_relations( $post_id );
$this->assertFalse( has_category( 'category-1', $post_id ) );
}
/**
* Test for Term_Manager::update_schedule().
*/
public function test_update_schedule() {
$term_manager = new Term_Manager( 'schedule_terms', 'schedule_terms_active', strtotime( '2022-05-29T08:05:41+00:00' ) );
foreach ( range( 1, 2 ) as $index ) {
$category_id = $this->factory()->category->create( array( 'name' => 'category-' . $index ) );
update_term_meta( $category_id, 'schedule_terms_active', true );
}
$post_id = $this->factory()->post->create();
add_post_meta(
$post_id,
'schedule_terms',
array(
'type' => 'attach',
'taxonomy' => 'category',
'term' => 'category-1',
'datetime' => '2022-05-29T08:05:40+00:00',
),
false
);
add_post_meta(
$post_id,
'schedule_terms',
array(
'type' => 'detach',
'taxonomy' => 'category',
'term' => 'category-1',
'datetime' => '2022-05-29T08:05:42+00:00',
),
false
);
add_post_meta(
$post_id,
'schedule_terms',
array(
'type' => 'attach',
'taxonomy' => 'category',
'term' => 'category-2',
'datetime' => '2022-05-29T08:05:42+00:00',
),
false
);
add_post_meta(
$post_id,
'schedule_terms',
array(
'type' => 'detach',
'taxonomy' => 'category',
'term' => 'category-2',
'datetime' => '2022-05-29T08:05:43+00:00',
),
false
);
$term_manager->update_schedule( $post_id );
$events = array_filter(
self::get_events(),
function ( $event ) {
return Term_Manager::SCHEDULED_HOOK_NAME === $event->hook;
}
);
$this->assertEquals( 3, count( $events ) );
}
/**
* Get events.
*
* @return array
*/
public static function get_events(): array {
$crons = _get_cron_array();
$events = array();
foreach ( $crons as $time => $hooks ) {
foreach ( $hooks as $hook => $hook_events ) {
foreach ( $hook_events as $sig => $data ) {
$events[] = (object) array(
'hook' => $hook,
'time' => $time,
'sig' => $sig,
'args' => $data['args'],
'schedule' => $data['schedule'],
);
}
}
}
return $events;
}
}