<!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>[44906] trunk: Privacy: Allow new requests to be created after a user's existing one has been completed.</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 { white-space: pre-line; 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/44906">44906</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/44906","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>desrosj</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-03-15 16:26:55 +0000 (Fri, 15 Mar 2019)</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'>Privacy: Allow new requests to be created after a user's existing one has been completed.

When dealing with personal data exports and erasure requests, it is important to have a log of all the requests for a specific person. This is often required to confirm when and how many times requests were completed and fulfilled properly. 

This change allows a new request to be created after a previous data request has reached completed status (`request-completed`) instead of requiring admins to delete or re-initiate the existing request. The latter approach removes the historical log of requests for that user when creating a new request.

Full unit tests for the `wp_create_user_request()` function are also included.

Props garrett-eclipse, cc0a, birgire, desrosj.
Fixes <a href="https://core.trac.wordpress.org/ticket/44707">#44707</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesuserphp">trunk/src/wp-includes/user.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsprivacywpCreateUserRequestphp">trunk/tests/phpunit/tests/privacy/wpCreateUserRequest.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesuserphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/user.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/user.php    2019-03-15 14:53:18 UTC (rev 44905)
+++ trunk/src/wp-includes/user.php      2019-03-15 16:26:55 UTC (rev 44906)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3330,13 +3330,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'post_type'     => 'user_request',
</span><span class="cx" style="display: block; padding: 0 10px">                        'post_name__in' => array( $action_name ),  // Action name stored in post_name column.
</span><span class="cx" style="display: block; padding: 0 10px">                        'title'         => $email_address, // Email address stored in post_title column.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'post_status'   => 'any',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'post_status'   => array(
+                               'request-pending',
+                               'request-confirmed',
+                       ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         'fields'        => 'ids',
</span><span class="cx" style="display: block; padding: 0 10px">                )
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $requests_query->found_posts ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return new WP_Error( 'duplicate_request', __( 'A request for this email address already exists.' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return new WP_Error( 'duplicate_request', __( 'An incomplete request for this email address already exists.' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $request_id = wp_insert_post(
</span></span></pre></div>
<a id="trunktestsphpunittestsprivacywpCreateUserRequestphp"></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/privacy/wpCreateUserRequest.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/privacy/wpCreateUserRequest.php                         (rev 0)
+++ trunk/tests/phpunit/tests/privacy/wpCreateUserRequest.php   2019-03-15 16:26:55 UTC (rev 44906)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,298 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Test the `wp_create_user_request()` function.
+ *
+ * @package WordPress
+ * @subpackage UnitTests
+ * @since 5.2.0
+ */
+
+/**
+ * Tests_WpCreateUserRequest class.
+ *
+ * @group privacy
+ * @covers ::wp_create_user_request
+ *
+ * @since 5.2.0
+ */
+class Tests_WpCreateUserRequest extends WP_UnitTestCase {
+       /**
+        * Request ID.
+        *
+        * @since 5.2.0
+        *
+        * @var int $request_id
+        */
+       protected static $request_id;
+
+       /**
+        * Request email for a registered user.
+        *
+        * @since 5.2.0
+        *
+        * @var string $registered_user_email
+        */
+       protected static $registered_user_email;
+
+       /**
+        * Request email for a non-registered user.
+        *
+        * @since 5.2.0
+        *
+        * @var string $non_registered_user_email
+        */
+       protected static $non_registered_user_email;
+
+       /**
+        * Test user ID.
+        *
+        * @since 5.2.0
+        *
+        * @var string $user_id
+        */
+       protected static $user_id;
+
+       /**
+        * Create fixtures.
+        *
+        * @since 5.2.0
+        *
+        * @param WP_UnitTest_Factory $factory Factory.
+        */
+       public static function wpSetUpBeforeClass( $factory ) {
+               self::$registered_user_email     = 'export@local.test';
+               self::$non_registered_user_email = 'non-registered-user@local.test';
+
+               self::$user_id = $factory->user->create(
+                       array(
+                               'user_email' => self::$registered_user_email,
+                       )
+               );
+
+               self::$request_id = $factory->post->create(
+                       array(
+                               'post_type'   => 'user_request',
+                               'post_author' => self::$user_id,
+                               'post_name'   => 'export_personal_data',
+                               'post_status' => 'request-pending',
+                               'post_title'  => self::$registered_user_email,
+                       )
+               );
+       }
+
+       /**
+        * Ensure a WP_Error is returned when an invalid email is passed.
+        *
+        * @ticket 44707
+        */
+       public function test_invalid_email() {
+               $actual = wp_create_user_request( 'not-a-valid-email', 'export_personal_data' );
+
+               $this->assertWPError( $actual );
+               $this->assertSame( 'invalid_email', $actual->get_error_code() );
+       }
+
+       /**
+        * Ensure a WP_Error is returned when an invalid action is passed.
+        *
+        * @ticket 44707
+        */
+       public function test_invalid_action() {
+               $actual = wp_create_user_request( self::$registered_user_email, false );
+
+               $this->assertWPError( $actual );
+               $this->assertSame( 'invalid_action', $actual->get_error_code() );
+       }
+
+       /**
+        * When there are incomplete requests for a registered user, a WP_Error should be returned.
+        *
+        * @ticket 44707
+        */
+       public function test_failure_due_to_incomplete_registered_user() {
+               // Second request (duplicated).
+               $actual = wp_create_user_request( self::$registered_user_email, 'export_personal_data' );
+
+               $this->assertWPError( $actual );
+               $this->assertSame( 'duplicate_request', $actual->get_error_code() );
+       }
+
+       /**
+        * When there are incomplete requests for an non-registered user, a WP_Error should be returned.
+        *
+        * @ticket 44707
+        */
+       public function test_failure_due_to_incomplete_unregistered_user() {
+               // Update first request.
+               wp_update_post(
+                       array(
+                               'ID'          => self::$request_id,
+                               'post_author' => 0,
+                               'post_title'  => self::$non_registered_user_email,
+                       )
+               );
+
+               // Second request (duplicated).
+               $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data' );
+
+               $this->assertWPError( $actual );
+               $this->assertSame( 'duplicate_request', $actual->get_error_code() );
+       }
+
+       /**
+        * Ensure emails are properly sanitized.
+        *
+        * @ticket 44707
+        */
+       public function test_sanitized_email() {
+               $actual = wp_create_user_request( 'some(email<withinvalid\characters@local.test', 'export_personal_data' );
+
+               $this->assertNotWPError( $actual );
+
+               $post = get_post( $actual );
+
+               $this->assertSame( 'export_personal_data', $post->post_name );
+               $this->assertSame( 'someemailwithinvalidcharacters@local.test', $post->post_title );
+       }
+
+       /**
+        * Ensure action names are properly sanitized.
+        *
+        * @ticket 44707
+        */
+       public function test_sanitized_action_name() {
+               $actual = wp_create_user_request( self::$non_registered_user_email, 'some[custom*action\name' );
+
+               $this->assertNotWPError( $actual );
+
+               $post = get_post( $actual );
+
+               $this->assertSame( 'somecustomactionname', $post->post_name );
+               $this->assertSame( self::$non_registered_user_email, $post->post_title );
+       }
+
+       /**
+        * Test a user request is created successfully for a registered user.
+        *
+        * @ticket 44707
+        */
+       public function test_create_request_registered_user() {
+               wp_delete_post( self::$request_id, true );
+
+               $test_data = array(
+                       'test-data'  => 'test value here',
+                       'test index' => 'more privacy data',
+               );
+
+               $actual = wp_create_user_request( self::$registered_user_email, 'export_personal_data', $test_data );
+
+               $this->assertNotWPError( $actual );
+
+               $post = get_post( $actual );
+
+               $this->assertSame( self::$user_id, (int) $post->post_author );
+               $this->assertSame( 'export_personal_data', $post->post_name );
+               $this->assertSame( self::$registered_user_email, $post->post_title );
+               $this->assertSame( 'request-pending', $post->post_status );
+               $this->assertSame( 'user_request', $post->post_type );
+               $this->assertSame( wp_json_encode( $test_data ), $post->post_content );
+       }
+
+       /**
+        * Test a user request is created successfully for an non-registered user.
+        *
+        * @ticket 44707
+        */
+       public function test_create_request_unregistered_user() {
+               wp_delete_post( self::$request_id, true );
+
+               $test_data = array(
+                       'test-data'  => 'test value here',
+                       'test index' => 'more privacy data',
+               );
+
+               $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data', $test_data );
+
+               $this->assertNotWPError( $actual );
+
+               $post = get_post( $actual );
+
+               $this->assertSame( 0, (int) $post->post_author );
+               $this->assertSame( 'export_personal_data', $post->post_name );
+               $this->assertSame( self::$non_registered_user_email, $post->post_title );
+               $this->assertSame( 'request-pending', $post->post_status );
+               $this->assertSame( 'user_request', $post->post_type );
+               $this->assertSame( wp_json_encode( $test_data ), $post->post_content );
+       }
+
+       /**
+        * Test that a pre-existing request for the same registered user that is not pending or confirmed status does not
+        * block a new request.
+        *
+        * @ticket 44707
+        */
+       public function test_completed_request_does_not_block_new_request() {
+               // Update first request.
+               wp_update_post(
+                       array(
+                               'ID'          => self::$request_id,
+                               'post_status' => 'request-completed', // Not 'request-pending' or 'request-confirmed'.
+                       )
+               );
+
+               // Second request.
+               $actual = wp_create_user_request( self::$registered_user_email, 'export_personal_data' );
+
+               $this->assertNotWPError( $actual );
+
+               $post = get_post( $actual );
+
+               $this->assertSame( self::$registered_user_email, $post->post_title );
+               $this->assertSame( 'request-pending', $post->post_status );
+               $this->assertSame( 'user_request', $post->post_type );
+       }
+
+       /**
+        * Test that a pre-existing request for the same non-registered user that is not pending or confirmed status does not
+        * block a new request.
+        *
+        * @ticket 44707
+        */
+       public function test_completed_request_does_not_block_new_request_for_unregistered_user() {
+               wp_update_post(
+                       array(
+                               'ID'          => self::$request_id,
+                               'post_author' => 0,
+                               'post_title'  => self::$non_registered_user_email,
+                               'post_status' => 'request-failed', // Not 'request-pending' or 'request-confirmed'.
+                       )
+               );
+
+               $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data' );
+
+               $this->assertNotWPError( $actual );
+
+               $post = get_post( $actual );
+
+               $this->assertSame( 0, (int) $post->post_author );
+               $this->assertSame( 'export_personal_data', $post->post_name );
+               $this->assertSame( self::$non_registered_user_email, $post->post_title );
+               $this->assertSame( 'request-pending', $post->post_status );
+               $this->assertSame( 'user_request', $post->post_type );
+       }
+
+       /**
+        * Test that an error from `wp_insert_post()` is returned.
+        *
+        * @ticket 44707
+        */
+       public function test_wp_error_returned_from_wp_insert_post() {
+               wp_delete_post( self::$request_id, true );
+
+               add_filter( 'wp_insert_post_empty_content', '__return_true' );
+               $actual = wp_create_user_request( self::$registered_user_email, 'export_personal_data' );
+
+               $this->assertWPError( $actual );
+               $this->assertSame( 'empty_content', $actual->get_error_code() );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/privacy/wpCreateUserRequest.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>

</body>
</html>