Skip to content

Commit

Permalink
add update schedules test
Browse files Browse the repository at this point in the history
  • Loading branch information
torounit committed May 29, 2022
1 parent 88b5cc1 commit 7d5b08c
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 10 deletions.
4 changes: 2 additions & 2 deletions includes/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
class Schedule {

const ATTACH = 'attach';
const DETACH = 'detach';
public const ATTACH = 'attach';
public const DETACH = 'detach';

/**
* Schedule tyoe.
Expand Down
9 changes: 6 additions & 3 deletions includes/Term_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
class Term_Manager {

public const SCHEDULED_HOOK_NAME = 'schedule_terms_update_post_term_relations';

/**
* Post meta key for save term info.
*
Expand Down Expand Up @@ -49,7 +51,7 @@ public function __construct( string $post_meta_key, string $term_meta_key, int $
$this->time = $time ?? time();
add_action( 'wp_after_insert_post', array( $this, 'update_post_term_relations' ), 100, 1 );
add_action( 'wp_after_insert_post', array( $this, 'update_schedule' ), 100, 1 );
add_action( 'schedule_terms_update_post_term_relations', array( $this, 'update_post_term_relations' ), 10, 4 );
add_action( self::SCHEDULED_HOOK_NAME, array( $this, 'update_post_term_relations' ), 10, 4 );
}

/**
Expand All @@ -63,6 +65,7 @@ private function is_active_term( ?WP_Term $term ): bool {
if ( ! $term ) {
return false;
}

return ! ! get_term_meta( $term->term_id, $this->term_meta_key, true );
}

Expand Down Expand Up @@ -139,8 +142,8 @@ public function update_schedule( int $post_id ) {
if ( ! $schedule->is_expired( $this->time ) ) {
$time = $schedule->get_timestamp();
$params = array( $post_id, array( $schedule->get_type() ), $schedule->get_taxonomy(), $schedule->get_term() );
wp_clear_scheduled_hook( 'schedule_terms_update_post_term_relations', $params );
wp_schedule_single_event( $time, 'schedule_terms_update_post_term_relations', $params );
wp_clear_scheduled_hook( self::SCHEDULED_HOOK_NAME, $params );
wp_schedule_single_event( $time, self::SCHEDULED_HOOK_NAME, $params );
}
}
}
Expand Down
112 changes: 107 additions & 5 deletions tests/Term_Manager_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace HAMWORKS\WP\Schedule_Terms\Tests;

use HAMWORKS\WP\Schedule_Terms\Term_Manager;
use PHPUnit\Framework\TestCase;
use WP_UnitTestCase;

/**
Expand All @@ -20,7 +19,7 @@ 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' );
$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 );
Expand All @@ -33,22 +32,125 @@ public function test_update_post_term_relations() {
'type' => 'attach',
'taxonomy' => 'category',
'term' => 'category-1',
'datetime' => '2012-07-05T22:09:28+09:00',
'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() {
$this->assertEquals( 1, 1 );
$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;
}

}

0 comments on commit 7d5b08c

Please sign in to comment.