User Group Content Restriction 03

Reworking the term manager in light of the taxonomy dual nature.

Not the same taxonomy

As written in an earlier post the same taxonomy can not be used to assign terms to posts and users.
That would have been convenient while developing the User Groups Content Restriction plugin but that’s an obstacle that can be overcame with some refactoring.

Keeping terms in sync

I’m registering a new taxonomy, user-group being the one used by the User Groups plugin, to assign terms to the post types that will be restricted on a user group base.
Terms from the user-group taxonomy (applying to users) will be kept in sync with terms of this new taxonomy (applying to posts) and two additional terms will be added to represent “virtual user groups”: site visitors and logged in users.


Since I had to modify the previously written tests to keep the code in sync with the needed modifications I’ve gone and tried to dig a little into the possibilities offered by WordPress test case factories and found out how useful those could be.


class ugcr_TermManagerTest extends \WP_UnitTestCase {

    protected $backupGlobals = false;

     * @var ugcr_TermManager
    protected $sut;

    public function setUp() {

        // before

        // activate the plugin
        activate_plugin( 'user-groups-content-restriction/user-groups-content-restriction.php' );

        // register the taxonomies that will come into play
        register_taxonomy( 'user-group', 'user' );
        register_taxonomy( 'post-user-group', 'post' );

        // set up ad-hoc factories
        $this->factory->user_group = new WP_UnitTest_Factory_For_Term( $this->factory, 'user-group' );
        $this->factory->post_user_group = new WP_UnitTest_Factory_For_Term( $this->factory, 'post-user-group' );

        // subject under test
        $this->sut = new ugcr_TermManager();

    public function tearDown() {

        // your tear down methods here

        // then

    // ...more tests here...

     * @test
     * it should add a term to the post taxonomy when added to the user taxonomy
    public function it_should_add_a_term_to_the_post_taxonomy_when_added_to_the_user_taxonomy() {
        $user_term = $this->factory->user_group->create_and_get();

        $this->sut->create_term( $user_term->term_id, $user_term->term_taxonomy_id );

        $post_terms = get_terms( 'post-user-group', [ 'hide_empty' => false ] );
        $user_terms = get_terms( 'user-group', [ 'hide_empty' => false ] );
        $this->assertCount( 1, $user_terms );
        $this->assertCount( 1, $post_terms );
        $post_term = $post_terms[0];
        $this->assertEquals( $user_term->slug, $post_term->slug );
        $this->assertEquals( $user_term->name . ' ' . __( 'group', 'ugcr' ), $post_term->name );

     * @test
     * it should remove the term from the post taxonomy when removed from the user group taxonomy
    public function it_should_remove_the_term_from_the_post_taxonomy_when_removed_from_the_user_group_taxonomy() {
        $user_term = $this->factory->user_group->create_and_get();
        $pugt      = $this->factory->post_user_group->create_and_get( array(
            'slug' => $user_term->slug,
            'name' => $user_term->name . ' ' . __( 'group', 'ugcr' )
        ) );
        $this->assertCount( 1, get_terms( 'post-user-group', [ 'hide_empty' => false ] ) );

        $this->sut->delete_term( $pugt->term_id, $pugt->term_taxonomy_id, $user_term );

        $this->assertCount( 0, get_terms( 'post-user-group', [ 'hide_empty' => false ] ) );

I’ve remvoed tests that have nothing particular and left those where I’m using WP_UnitTestCase factories to some extent.
Code is on GitHub.


Time to finish the plugin and put to use. The UI part is missing but that should be a quick task.

I appreciate your input