Skip to content

Instantly share code, notes, and snippets.

@alexkingorg
Created September 20, 2012 20:53
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save alexkingorg/3758299 to your computer and use it in GitHub Desktop.
WordPress unit test to verify that slashed data is saved properly for post meta
<?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 ) );
}
}
@scribu
Copy link

scribu commented Sep 20, 2012

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

@scribu
Copy link

scribu commented Sep 20, 2012

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 );

@scribu
Copy link

scribu commented Sep 20, 2012

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

@alexkingorg
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment