public

WordPress unit test to verify that slashed data is saved properly for post meta

  • Download Gist
wp-test-postmeta-slash.php
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 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
<?php
 
/**
* @group meta
* @group slashes
* @ticket 21767
*/
class Tests_Meta_Slashes extends WP_UnitTestCase {
function setUp() {
parent::setUp();
$this->author_id = $this->factory->user->create( array( 'role' => 'editor' ) );
$this->old_current_user = get_current_user_id();
wp_set_current_user( $this->author_id );
 
$this->slash_1 = 'String with 1 slash \\';
$this->slash_2 = 'String with 2 slashes \\\\';
$this->slash_3 = 'String with 3 slashes \\\\\\';
$this->slash_4 = 'String with 4 slashes \\\\\\\\';
$this->slash_5 = 'String with 5 slashes \\\\\\\\\\';
$this->slash_6 = 'String with 6 slashes \\\\\\\\\\\\';
$this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
}
 
function tearDown() {
wp_set_current_user( $this->old_current_user );
parent::tearDown();
}
 
/**
* Tests the controller function that expects slashed data
*
*/
function test_edit_post() {
$id = $this->factory->post->create();
if ( function_exists( 'wp_add_post_meta' ) ) {
$meta_1 = wp_add_post_meta( $id, 'slash_test_1', 'foo' );
$meta_2 = wp_add_post_meta( $id, 'slash_test_2', 'foo' );
$meta_3 = wp_add_post_meta( $id, 'slash_test_3', 'foo' );
}
else {
// expects slashed data
$meta_1 = add_post_meta( $id, 'slash_test_1', addslashes( 'foo' ) );
$meta_2 = add_post_meta( $id, 'slash_test_2', addslashes( 'foo' ) );
$meta_3 = add_post_meta( $id, 'slash_test_3', addslashes( 'foo' ) );
}
 
$_POST = array();
$_POST['post_ID'] = $id;
$_POST['metakeyselect'] = '#NONE#';
$_POST['metakeyinput'] = 'slash_test_0';
$_POST['metavalue'] = $this->slash_6;
$_POST['meta'] = array(
$meta_1 => array(
'key' => 'slash_test_1',
'value' => $this->slash_1
),
$meta_2 => array(
'key' => 'slash_test_2',
'value' => $this->slash_3
),
$meta_3 => array(
'key' => 'slash_test_3',
'value' => $this->slash_4
),
);
$_POST = add_magic_quotes( $_POST ); // the edit_post() function will strip slashes
 
edit_post();
$post = get_post( $id );
 
$this->assertEquals( $this->slash_6, get_post_meta( $id, 'slash_test_0', true ) );
$this->assertEquals( $this->slash_1, get_post_meta( $id, 'slash_test_1', true ) );
$this->assertEquals( $this->slash_3, get_post_meta( $id, 'slash_test_2', true ) );
$this->assertEquals( $this->slash_4, get_post_meta( $id, 'slash_test_3', true ) );
 
$_POST = array();
$_POST['post_ID'] = $id;
$_POST['metakeyselect'] = '#NONE#';
$_POST['metakeyinput'] = 'slash_test_0';
$_POST['metavalue'] = $this->slash_7;
$_POST['meta'] = array(
$meta_1 => array(
'key' => 'slash_test_1',
'value' => $this->slash_2
),
$meta_2 => array(
'key' => 'slash_test_2',
'value' => $this->slash_4
),
$meta_3 => array(
'key' => 'slash_test_3',
'value' => $this->slash_5
),
);
$_POST = add_magic_quotes( $_POST ); // the edit_post() function will strip slashes
 
edit_post();
$post = get_post( $id );
 
$this->assertEquals( $this->slash_2, get_post_meta( $id, 'slash_test_1', true ) );
$this->assertEquals( $this->slash_4, get_post_meta( $id, 'slash_test_2', true ) );
$this->assertEquals( $this->slash_5, get_post_meta( $id, 'slash_test_3', true ) );
}
 
/**
* Tests the legacy model function that expects slashed data
*
*/
function test_add_post_meta() {
$id = $this->factory->post->create();
add_post_meta( $id, 'slash_test_1', addslashes( $this->slash_1 ) );
add_post_meta( $id, 'slash_test_2', addslashes( $this->slash_3 ) );
add_post_meta( $id, 'slash_test_3', addslashes( $this->slash_4 ) );
 
$this->assertEquals( $this->slash_1, get_post_meta( $id, 'slash_test_1', true ) );
$this->assertEquals( $this->slash_3, get_post_meta( $id, 'slash_test_2', true ) );
$this->assertEquals( $this->slash_4, get_post_meta( $id, 'slash_test_3', true ) );
}
 
/**
* Tests the legacy model function that expects slashed data
*
*/
function test_update_post_meta() {
$id = $this->factory->post->create();
update_post_meta( $id, 'slash_test_1', addslashes( $this->slash_1 ) );
update_post_meta( $id, 'slash_test_2', addslashes( $this->slash_3 ) );
update_post_meta( $id, 'slash_test_3', addslashes( $this->slash_4 ) );
 
$this->assertEquals( $this->slash_1, get_post_meta( $id, 'slash_test_1', true ) );
$this->assertEquals( $this->slash_3, get_post_meta( $id, 'slash_test_2', true ) );
$this->assertEquals( $this->slash_4, get_post_meta( $id, 'slash_test_3', true ) );
}
 
/**
* Tests the model function that expects un-slashed data
*
*/
function test_wp_add_post_meta() {
if ( !function_exists( 'wp_add_post_meta' ) ) {
return;
}
$id = $this->factory->post->create();
wp_add_post_meta( $id, 'slash_test_1', $this->slash_1 );
wp_add_post_meta( $id, 'slash_test_2', $this->slash_3 );
wp_add_post_meta( $id, 'slash_test_3', $this->slash_4 );
 
$this->assertEquals( $this->slash_1, get_post_meta( $id, 'slash_test_1', true ) );
$this->assertEquals( $this->slash_3, get_post_meta( $id, 'slash_test_2', true ) );
$this->assertEquals( $this->slash_4, get_post_meta( $id, 'slash_test_3', true ) );
}
 
/**
* Tests the model function that expects un-slashed data
*
*/
function test_wp_update_post_meta() {
if ( !function_exists( 'wp_update_post_meta' ) ) {
return;
}
$id = $this->factory->post->create();
wp_update_post_meta( $id, 'slash_test_1', $this->slash_1 );
wp_update_post_meta( $id, 'slash_test_2', $this->slash_3 );
wp_update_post_meta( $id, 'slash_test_3', $this->slash_4 );
 
$this->assertEquals( $this->slash_1, get_post_meta( $id, 'slash_test_1', true ) );
$this->assertEquals( $this->slash_3, get_post_meta( $id, 'slash_test_2', true ) );
$this->assertEquals( $this->slash_4, get_post_meta( $id, 'slash_test_3', true ) );
}
 
/**
* Tests the model function that expects un-slashed data
*
*/
function test_add_comment_meta() {
$id = $this->factory->comment->create();
 
add_comment_meta( $id, 'slash_test_1', $this->slash_1 );
add_comment_meta( $id, 'slash_test_2', $this->slash_3 );
add_comment_meta( $id, 'slash_test_3', $this->slash_5 );
 
$this->assertEquals( $this->slash_1, get_comment_meta( $id, 'slash_test_1', true ) );
$this->assertEquals( $this->slash_3, get_comment_meta( $id, 'slash_test_2', true ) );
$this->assertEquals( $this->slash_5, get_comment_meta( $id, 'slash_test_3', true ) );
 
add_comment_meta( $id, 'slash_test_4', $this->slash_2 );
add_comment_meta( $id, 'slash_test_5', $this->slash_4 );
add_comment_meta( $id, 'slash_test_6', $this->slash_6 );
 
$this->assertEquals( $this->slash_2, get_comment_meta( $id, 'slash_test_4', true ) );
$this->assertEquals( $this->slash_4, get_comment_meta( $id, 'slash_test_5', true ) );
$this->assertEquals( $this->slash_6, get_comment_meta( $id, 'slash_test_6', true ) );
}
 
/**
* Tests the model function that expects un-slashed data
*
*/
function test_update_comment_meta() {
$id = $this->factory->comment->create();
 
add_comment_meta( $id, 'slash_test_1', 'foo' );
add_comment_meta( $id, 'slash_test_2', 'foo' );
add_comment_meta( $id, 'slash_test_3', 'foo' );
 
update_comment_meta( $id, 'slash_test_1', $this->slash_1 );
update_comment_meta( $id, 'slash_test_2', $this->slash_3 );
update_comment_meta( $id, 'slash_test_3', $this->slash_5 );
 
$this->assertEquals( $this->slash_1, get_comment_meta( $id, 'slash_test_1', true ) );
$this->assertEquals( $this->slash_3, get_comment_meta( $id, 'slash_test_2', true ) );
$this->assertEquals( $this->slash_5, get_comment_meta( $id, 'slash_test_3', true ) );
 
update_comment_meta( $id, 'slash_test_1', $this->slash_2 );
update_comment_meta( $id, 'slash_test_2', $this->slash_4 );
update_comment_meta( $id, 'slash_test_3', $this->slash_6 );
 
$this->assertEquals( $this->slash_2, get_comment_meta( $id, 'slash_test_1', true ) );
$this->assertEquals( $this->slash_4, get_comment_meta( $id, 'slash_test_2', true ) );
$this->assertEquals( $this->slash_6, get_comment_meta( $id, 'slash_test_3', true ) );
}
 
}

Why not make slash_1, slash_2, etc. class constants?

Also, I think it would be less confusing if meta keys matched slash values:

wp_add_post_meta( $id, 'slash_test_1', $this->slash_1 );
wp_add_post_meta( $id, 'slash_test_2', $this->slash_2 );
wp_add_post_meta( $id, 'slash_test_3', $this->slash_3 );

I heard that you don't get comment notifications for gists anymore, unless I mention you, @alexkingorg.

Man, still didn't get any notifications - just happened to poke back in here. The tests run even and odd slashes in groups specifically.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.