CMB2 Pipes taking on users and posts relations

A road map for the plugin evolution in the Posts 2 Posts context.

The current state

The now available version og CMB2 Pipes will handle piping to/from Posts 2 Posts relation between posts without a problem.
The challenge is now to make it work as well when connecting users to posts and users to users: since the Posts 2 Posts allows for that it only makes sense to have that possibility implemented in the plugin as well.

Different scenarios

When dealing with posts to posts relations there are not many places where the piping might happen but the post edit screen; when it comes to users three more scenarios open:

  • the piping might happen on a post edit screen and relate a user and a post
  • the piping might happen on a user edit screen and relate a user and a post
  • the piping might happen on a user edit screen and relate a user and a user

I do not have a deep enough knowledge of Custom Meta Boxes 2 inner workings to know what might differ from one scenario to another and am hoping testing the functions will shine some light in that context.
Plus that each pipe can have one of three piping options (read, write, read and write) multiplies the scenarios.

A first test

I’ve laid down a first test to spot fixture set up difficulties early on and am quite surprised that it did not result in an error:


class P2PPipeUserToPostOnUserTest extends \WP_UnitTestCase {

        public static function setUpBeforeClass() {
            load_p2p();
            p2p_register_connection_type( [
                'name' => 'user_to_post',
                'from' => 'user',
                'to'   => 'post'
            ] );
        }

        /**
         * @test
         * it should allow piping to a user to post p2p relation
         */
        public function it_should_allow_piping_to_a_user_to_post_p_2_p_relation() {
            $user_id = $this->factory->user->create();
            $post_id = $this->factory->post->create();

            $args = [
                'object_id'   => $user_id,
                'object_type' => 'user',
                'field_args'  => [
                    'name' => __( 'Relate a user with a post', 'cmb2' ),
                    'id'   => cmb2_p2p_pipe( 'user_field', '', 'user_to_post' ),
                    'type' => 'text' // not relevant
                ]
            ];

            $field = new CMB2_Field( $args );

            $field->save_field( $post_id );

            /** @var \wpdb $wpdb */
            global $wpdb;
            $this->assertEquals( [ $post_id ], $wpdb->get_col( "select p2p_to from $wpdb->p2p where p2p_from = $user_id" ) );
        }
    }

A first user and post relating pipe passing
And while I cannot praise the WordPress core team enough for supplying utility methods like


$user_id = $this->factory->user->create();
$post_id = $this->factory->post->create();

I will move on with the tests learning by failure.

I appreciate your input