-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathtest-batcache-manager.php
More file actions
113 lines (92 loc) · 2.54 KB
/
test-batcache-manager.php
File metadata and controls
113 lines (92 loc) · 2.54 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
<?php
/**
* Tests for the Batcache Manager plugin (batcache.php).
*
* @package Batcache
*/
class Test_Batcache_Manager extends WP_UnitTestCase {
public function setUp() {
global $batcache, $wp_object_cache;
$batcache = new stdClass;
$batcache->group = 'batcache';
// Initialize the cache and add an undefined property.
wp_cache_init();
$wp_object_cache->no_remote_groups = array();
parent::setUp();
}
public function test_batcache_post() {
$post_id = self::factory()->post->create();
$home = trailingslashit( get_option( 'home' ) );
$urls = array(
$home,
$home . 'feed/',
get_permalink( $post_id )
);
// Seed the cache.
foreach ( $urls as $url ) {
$this->set_cache( $url, uniqid() );
}
// Run batcache_post().
batcache_post( $post_id );
// All three caches should now be empty.
foreach ( $urls as $url ) {
//$this->assertFalse( $this->get_cache( $url ) );
}
}
/**
* Revisions shouldn't change anything in the cache.
*/
public function test_batcache_post_skips_revisions() {
$post_id = self::factory()->post->create( array(
'post_type' => 'revision'
) );
$url = get_permalink( $post_id );
$val = uniqid();
// Seed the cache.
$this->set_cache( $url, $val );
// After running batcache_post(), our value should still be set.
batcache_post( $post_id );
$this->assertEquals( $val, $this->get_cache( $url ) );
}
/**
* Only published and trashed post statuses should qualify.
*/
public function test_batcache_post_skips_drafts() {
$post_id = self::factory()->post->create( array(
'post_status' => 'draft'
) );
$url = get_permalink( $post_id );
$val = uniqid();
// Seed the cache.
$this->set_cache( $url, $val );
// After running batcache_post(), our value should still be set.
batcache_post( $post_id );
$this->assertEquals( $val, $this->get_cache( $url ) );
}
/**
* Retrieve the contents of a seeded cache.
*
* @global $batcache
*
* @param string $url The URL to create a cache entry for.
* @return mixed The value previously stored in the cache.
*/
protected function get_cache( $url ) {
global $batcache;
$url_key = md5( $url );
return wp_cache_get( $url_key, $batcache->group );
}
/**
* Helper to put a URL into Batcache.
*
* @global $batcache
*
* @param string $url The URL to create a cache entry for.
* @param string $val The value to store in the cache.
*/
protected function set_cache( $url, $val ) {
global $batcache;
$url_key = md5( $url );
wp_cache_add( $url_key, $val, $batcache->group );
}
}