<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[37995] trunk/tests/phpunit/tests/meta/registerMeta.php: Meta: Introduce an initial set of tests for `register_meta()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/37995">37995</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/37995","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jeremyfelt</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-07-07 00:14:40 +0000 (Thu, 07 Jul 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Meta: Introduce an initial set of tests for `register_meta()`.

Props sc0ttkclark, ericlewis.
See <a href="https://core.trac.wordpress.org/ticket/35658">#35658</a>.</pre>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsmetaregisterMetaphp">trunk/tests/phpunit/tests/meta/registerMeta.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsmetaregisterMetaphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/meta/registerMeta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/meta/registerMeta.php                           (rev 0)
+++ trunk/tests/phpunit/tests/meta/registerMeta.php     2016-07-07 00:14:40 UTC (rev 37995)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,362 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * @group meta
+ */
+class Tests_Meta_Register_Meta extends WP_UnitTestCase {
+       protected static $editor_id;
+       protected static $post_id;
+       protected static $comment_id;
+
+       public static function wpSetUpBeforeClass( $factory ) {
+               self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
+               self::$post_id = $factory->post->create();
+               self::$comment_id = $factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
+       }
+
+       public static function wpTearDownAfterClass() {
+               self::delete_user( self::$editor_id );
+               wp_delete_comment( self::$comment_id, true );
+               wp_delete_post( self::$post_id, true );
+       }
+
+       function setUp() {
+               parent::setUp();
+               wp_set_current_user( self::$editor_id );
+       }
+
+       public function _old_sanitize_meta_cb( $meta_value, $meta_key, $meta_type ) {
+               return $meta_key . ' old sanitized';
+       }
+
+       public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type, $object_subtype ) {
+               return $meta_key . ' new sanitized';
+       }
+
+       public function test_register_meta_with_valid_object_type_and_object_subtype_returns_true() {
+               $result = register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
+               unregister_meta_key( 'post', 'post', 'flight_number' );
+
+               $this->assertTrue( $result );
+       }
+
+       public function test_register_meta_with_post_object_type_and_subtype_populates_wp_meta_keys() {
+               global $wp_meta_keys;
+
+               register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
+               $actual = $wp_meta_keys;
+               unregister_meta_key( 'post', 'post', 'flight_number' );
+
+               $expected = array(
+                       'post' => array(
+                               'post' => array(
+                                       'flight_number' => array(
+                                               'object_subtype' => 'post',
+                                               'type' => 'string',
+                                               'description' => '',
+                                               'single' => false,
+                                               'sanitize_callback' => null,
+                                               'auth_callback' => '__return_true',
+                                               'show_in_rest' => false,
+                                       ),
+                               ),
+                       ),
+               );
+
+               $this->assertEquals( $actual, $expected );
+       }
+
+       public function test_register_meta_with_post_object_type_and_unregistered_subtype_populates_wp_meta_keys() {
+               global $wp_meta_keys;
+
+               register_meta( 'post', 'flight_number', array( 'object_subtype' => 'not_a_post_type' ) );
+               $actual = $wp_meta_keys;
+               unregister_meta_key( 'post', 'not_a_post_type', 'flight_number' );
+
+               $expected = array(
+                       'post' => array(
+                               'not_a_post_type' => array(
+                                       'flight_number' => array(
+                                               'object_subtype' => 'not_a_post_type',
+                                               'type' => 'string',
+                                               'description' => '',
+                                               'single' => false,
+                                               'sanitize_callback' => null,
+                                               'auth_callback' => '__return_true',
+                                               'show_in_rest' => false,
+                                       ),
+                               ),
+                       ),
+               );
+
+               $this->assertEquals( $actual, $expected );
+       }
+
+       public function test_register_meta_with_term_object_type_and_category_subtype_populates_wp_meta_keys() {
+               global $wp_meta_keys;
+               register_meta( 'term', 'category_icon', array( 'object_subtype' => 'category' ) );
+               $actual = $wp_meta_keys;
+               unregister_meta_key( 'term', 'category', 'category_icon' );
+
+               $expected = array(
+                       'term' => array(
+                               'category' => array(
+                                       'category_icon' => array(
+                                               'object_subtype' => 'category',
+                                               'type' => 'string',
+                                               'description' => '',
+                                               'single' => false,
+                                               'sanitize_callback' => null,
+                                               'auth_callback' => '__return_true',
+                                               'show_in_rest' => false,
+                                       ),
+                               ),
+                       ),
+               );
+
+               $this->assertEquals( $actual, $expected );
+       }
+
+       public function test_register_meta_with_comment_object_type_and_subtype_populates_wp_meta_keys() {
+               global $wp_meta_keys;
+               register_meta( 'comment', 'comment_rating', array( 'object_subtype' => 'comment' ) );
+               $actual = $wp_meta_keys;
+               unregister_meta_key( 'comment', 'comment', 'comment_rating' );
+
+               $expected = array(
+                       'comment' => array(
+                               'comment' => array(
+                                       'comment_rating' => array(
+                                               'object_subtype' => 'comment',
+                                               'type' => 'string',
+                                               'description' => '',
+                                               'single' => false,
+                                               'sanitize_callback' => null,
+                                               'auth_callback' => '__return_true',
+                                               'show_in_rest' => false,
+                                       ),
+                               ),
+                       ),
+               );
+
+               $this->assertEquals( $actual, $expected );
+       }
+
+       public function test_register_meta_with_deprecated_sanitize_callback_does_not_populate_wp_meta_keys() {
+               global $wp_meta_keys;
+
+               register_meta( 'post', 'flight_number', array( $this, '_old_sanitize_meta_cb' ) );
+               $actual = $wp_meta_keys;
+               remove_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb') );
+               remove_filter( 'auth_post_meta_flight_number', '__return_true');
+
+               $this->assertEquals( array(), $actual );
+       }
+
+       public function test_register_meta_with_deprecated_sanitize_callback_param_returns_wp_error() {
+               $actual = register_meta( 'post', 'flight_number', array( $this, '_old_sanitize_meta_cb' ) );
+
+               remove_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb') );
+               remove_filter( 'auth_post_meta_flight_number', '__return_true');
+
+               $this->assertWPError( $actual );
+               $this->assertEquals( 'register_meta_failed', $actual->get_error_code() );
+       }
+
+       public function test_register_meta_with_deprecated_sanitize_callback_parameter_passes_through_filter() {
+               register_meta( 'post', 'old_sanitized_key', array( $this, '_old_sanitize_meta_cb' ) );
+               $meta = sanitize_meta( 'old_sanitized_key', 'unsanitized', 'post', 'post' );
+
+               remove_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb') );
+               remove_filter( 'auth_post_meta_flight_number', '__return_true');
+
+               $this->assertEquals( 'old_sanitized_key old sanitized', $meta );
+       }
+
+       public function test_register_meta_with_current_sanitize_callback_populates_wp_meta_keys() {
+               global $wp_meta_keys;
+               register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
+               $actual = $wp_meta_keys;
+               unregister_meta_key( 'post', 'post', 'flight_number' );
+
+               $expected = array(
+                       'post' => array(
+                               'post' => array(
+                                       'flight_number' => array(
+                                               'object_subtype' => 'post',
+                                               'type' => 'string',
+                                               'description' => '',
+                                               'single' => false,
+                                               'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ),
+                                               'auth_callback' => '__return_true',
+                                               'show_in_rest' => false,
+                                       ),
+                               ),
+                       ),
+               );
+               $this->assertEquals( $actual, $expected );
+       }
+
+       public function test_register_meta_with_current_sanitize_callback_returns_true() {
+               $result = register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
+               unregister_meta_key( 'post', 'post', 'flight_number' );
+
+               remove_filter( 'sanitize_post_post_meta_flight_number', array( $this, '_new_sanitize_meta_cb') );
+               remove_filter( 'auth_post_post_meta_flight_number', '__return_true');
+
+               $this->assertTrue( $result );
+       }
+
+       public function test_register_meta_with_new_sanitize_callback_parameter() {
+               register_meta( 'post', 'new_sanitized_key', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
+               $meta = sanitize_meta( 'new_sanitized_key', 'unsanitized', 'post', 'post' );
+
+               unregister_meta_key( 'post', 'post', 'new_sanitized_key' );
+
+               remove_filter( 'sanitize_post_post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb') );
+               remove_filter( 'auth_post_post_meta_new_sanitized_key', '__return_true');
+
+               $this->assertEquals( 'new_sanitized_key new sanitized', $meta );
+       }
+
+       public function test_unregister_meta_key_clears_key_from_wp_meta_keys() {
+               global $wp_meta_keys;
+               register_meta( 'post', 'registered_key', array( 'object_subtype' => 'post' ) );
+               unregister_meta_key( 'post', 'post', 'registered_key' );
+
+               $this->assertEquals( array(), $wp_meta_keys );
+       }
+
+       public function test_unregister_meta_key_with_invalid_key_returns_wp_error() {
+               $this->assertWPError( unregister_meta_key( 'post', 'post', 'not_a_registered_key' ) );
+       }
+
+       public function test_get_registered_meta_keys() {
+               register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
+               register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) );
+
+               $meta_keys = get_registered_meta_keys( 'post', 'post' );
+
+               unregister_meta_key( 'post', 'post', 'registered_key1' );
+               unregister_meta_key( 'post', 'post', 'registered_key2' );
+
+               $this->assertArrayHasKey( 'registered_key1', $meta_keys );
+               $this->assertArrayHasKey( 'registered_key2', $meta_keys );
+       }
+
+       public function test_get_registered_meta_keys_with_subtype_without_registered_keys_is_empty() {
+               register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
+               register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) );
+
+               $meta_keys = get_registered_meta_keys( 'post', 'page' );
+
+               unregister_meta_key( 'post', 'post', 'registered_key1' );
+               unregister_meta_key( 'post', 'post', 'registered_key2' );
+
+               $this->assertEmpty( $meta_keys );
+       }
+
+       public function test_get_registered_meta_keys_with_invalid_type_is_empty() {
+               register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
+               register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) );
+
+               $meta_keys = get_registered_meta_keys( 'invalid-type' );
+
+               unregister_meta_key( 'post', 'post', 'registered_key1' );
+               unregister_meta_key( 'post', 'post', 'registered_key2' );
+
+               $this->assertEmpty( $meta_keys );
+       }
+
+       public function test_get_registered_meta_keys_has_count() {
+               register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
+               register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'page' ) );
+
+               $meta_keys = get_registered_meta_keys( 'post' );
+
+               unregister_meta_key( 'post', 'post', 'registered_key1' );
+               unregister_meta_key( 'post', 'page', 'registered_key2' );
+
+               $this->assertCount( 2, $meta_keys );
+       }
+
+       public function test_get_registered_meta_keys_description_arg() {
+               register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post', 'description' => 'I\'m just a field, take a good look at me' ) );
+
+               $meta_keys = get_registered_meta_keys( 'post', 'post' );
+
+               unregister_meta_key( 'post', 'post', 'registered_key1' );
+
+               $this->assertEquals( 'I\'m just a field, take a good look at me', $meta_keys['registered_key1']['description'] );
+       }
+
+       public function test_get_registered_meta_keys_invalid_arg() {
+               register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post', 'invalid_arg' => 'invalid' ) );
+
+               $meta_keys = get_registered_meta_keys( 'post', 'post' );
+
+               unregister_meta_key( 'post', 'post', 'registered_key1' );
+
+               $this->assertArrayNotHasKey( 'invalid_arg', $meta_keys['registered_key1'] );
+       }
+
+       public function test_get_registered_metadata() {
+               register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
+               add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
+
+               $meta = get_registered_metadata( 'post', 'post', self::$post_id );
+
+               unregister_meta_key( 'post', 'post', 'flight_number' );
+
+               $this->assertEquals( 'Oceanic 815', $meta['flight_number'][0] );
+       }
+
+       public function test_get_registered_metadata_by_key() {
+               register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
+               add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
+
+               $meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_number' );
+
+               unregister_meta_key( 'post', 'post', 'flight_number' );
+
+               $this->assertEquals( 'Oceanic 815', $meta[0] );
+       }
+
+       public function test_get_registered_metadata_by_key_single() {
+               register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'single' => true ) );
+               add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
+
+               $meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_number' );
+
+               unregister_meta_key( 'post', 'post', 'flight_number' );
+
+               $this->assertEquals( 'Oceanic 815', $meta );
+       }
+
+       public function test_get_registered_metadata_by_invalid_key() {
+               register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
+               add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
+
+               $meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_pilot' );
+
+               unregister_meta_key( 'post', 'post', 'flight_number' );
+
+               $this->assertWPError( $meta );
+       }
+
+       public function test_get_registered_metadata_invalid_object_type() {
+               register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
+               add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
+
+               $meta = get_registered_metadata( 'invalid-type', 'invalid-subtype', self::$post_id );
+
+               unregister_meta_key( 'post', 'post', 'flight_number' );
+
+               $this->assertWPError( $meta );
+       }
+
+       public function test_get_registered_metadata_invalid() {
+               $meta = get_registered_metadata( 'invalid-type', 'invalid-subtype', self::$post_id );
+
+               $this->assertWPError( $meta );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>