diff --git a/composer.json b/composer.json index c2a3d5396..b04a148fe 100644 --- a/composer.json +++ b/composer.json @@ -14,17 +14,18 @@ "composer/installers": "~1.0" }, "require-dev": { - "johnpbloch/wordpress": "^5.4", "automattic/vipwpcs": "^2.0.0", + "humanmade/mercator": "^1.0", + "johnpbloch/wordpress": "^5.4", "php-coveralls/php-coveralls": "^2.1", "phpunit/phpunit": "^5.7", "wp-cli/wp-cli-bundle": "^2.2", "wp-coding-standards/wpcs": "^2.2", "wp-phpunit/wp-phpunit": "^5.4", - "wpsh/local": "^0.2.3", "wpackagist-plugin/advanced-custom-fields": "5.8.12", "wpackagist-plugin/easy-digital-downloads": "^2.9.23", - "wpackagist-plugin/user-switching": "^1.5.5" + "wpackagist-plugin/user-switching": "^1.5.5", + "wpsh/local": "^0.2.3" }, "config": { "process-timeout": 600, @@ -36,7 +37,8 @@ "extra": { "wordpress-install-dir": "local/public", "installer-paths": { - "local/public/wp-content/plugins/{$name}/": ["type:wordpress-plugin"] + "local/public/wp-content/plugins/{$name}/": ["type:wordpress-plugin"], + "local/public/wp-content/mu-plugins/{$name}/": ["type:wordpress-muplugin"] } }, "scripts": { diff --git a/composer.lock b/composer.lock index 38e0c8a80..5e2c65b45 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9ea430268ec38a2b87542c340bc6f5fe", + "content-hash": "c025a44ff83e162076363f15803df9da", "packages": [ { "name": "composer/installers", @@ -1036,6 +1036,46 @@ }, "time": "2020-09-30T07:37:11+00:00" }, + { + "name": "humanmade/mercator", + "version": "1.0.3", + "source": { + "type": "git", + "url": "https://github.com/humanmade/Mercator.git", + "reference": "21b0e838ff23d24bf023a74d1da42e063553d934" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/humanmade/Mercator/zipball/21b0e838ff23d24bf023a74d1da42e063553d934", + "reference": "21b0e838ff23d24bf023a74d1da42e063553d934", + "shasum": "" + }, + "require": { + "composer/installers": "~1.0" + }, + "type": "wordpress-muplugin", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0+" + ], + "authors": [ + { + "name": "Human Made Limited", + "email": "support@humanmade.co.uk", + "homepage": "http://hmn.md/" + } + ], + "description": "WordPress multisite domain mapping for the modern era.", + "homepage": "https://github.com/humanmade/Mercator", + "keywords": [ + "wordpress" + ], + "support": { + "issues": "https://github.com/humanmade/Mercator/issues", + "source": "https://github.com/humanmade/Mercator/tree/master" + }, + "time": "2018-10-30T18:53:20+00:00" + }, { "name": "johnpbloch/wordpress", "version": "5.6.0", diff --git a/docker-compose.yml b/docker-compose.yml index db46074e1..ca68d159e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,6 +31,7 @@ services: - ./local/config/wp-cli.yml:/var/www/html/wp-cli.yml - ./local/config/wp-config.php:/var/www/html/wp-config.php - ./local/config/wp-tests-config.php:/var/www/html/wp-tests-config.php + - ./local/config/sunrise.php:/var/www/html/wp-content/sunrise.php restart: always environment: COMPOSER_HOME: /tmp/.composer diff --git a/local/config/sunrise.php b/local/config/sunrise.php new file mode 100644 index 000000000..ac938fa4b --- /dev/null +++ b/local/config/sunrise.php @@ -0,0 +1,5 @@ +markTestSkipped( 'This test requires multisite.' ); + } + + // Add hook to provide mock blog details because sub-sites aren't + // created with an options table to use. + add_filter( 'site_details', array( $this, 'get_testsite_details' ) ); + + // Make partial of Connector_Mercator class, with mocked "log" function. + $this->mock = $this->getMockBuilder( Connector_Mercator::class ) + ->setMethods( array( 'log' ) ) + ->getMock(); + + // Register connector. + $this->mock->register(); + } + /** - * Holds the connector mercator base class. + * Mock function that define stub blog details that would be store in the blog options table + * which isn't created during tests. * - * @var Connector_Mercator + * @param object $details + * @return object */ - protected $connector_mercator; + public function get_testsite_details( $details ) { + global $base; - public function setUp() { - parent::setUp(); + $details->blogname = 'testsite'; + $details->siteurl = $base . '/testsite'; + $details->post_count = 0; + $details->home = $base . '/testsite'; - $this->connector_mercator = new Connector_Mercator; - $this->assertNotEmpty( $this->connector_mercator ); + return $details; } /** @@ -28,13 +54,186 @@ public function setUp() { * @group ms-required */ public function test_get_context_labels() { - if ( ! is_multisite() ) { - $this->markTestSkipped( 'This test requires multisite.' ); - } // Validate this works for foreign characters as well. $id = $this->factory->blog->create( array( 'title' => 'ובזכויותיהם' ) ); - $labels = $this->connector_mercator->get_context_labels(); + $labels = $this->mock->get_context_labels(); $this->assertArrayHasKey( 'blog-1', $labels ); $this->assertArrayHasKey( 'blog-' . $id , $labels ); } + + public function test_callback_mercator_mapping_made_primary() { + // Make blog and alias for later use. + $blog_id = $this->factory->blog->create( array( 'title' => 'testsite' ) ); + $mapping = \Mercator\Mapping::create( $blog_id, 'example.com' ); + + // Expected log calls. + $this->mock->expects( $this->exactly( 3 ) ) + ->method( 'log' ) + ->withConsecutive( + array( + $this->equalTo( + _x( + '"%1$s" domain alias was created for "%2$s"', + '1. Domain alias 2. Site name', + 'stream' + ) + ), + $this->equalTo( + array( + 'domain' => 'example.org', + 'site_name' => 'testsite', + ) + ), + $this->equalTo( $blog_id ), + $this->equalTo( 'testsite' ), + $this->equalTo( 'created' ), + ), + array( + $this->equalTo( + _x( + '"%1$s" domain alias was deleted for "%2$s"', + '1. Domain alias 2. Site name', + 'stream' + ) + ), + $this->equalTo( + array( + 'domain' => 'example.com', + 'site_name' => 'testsite', + ) + ), + $this->equalTo( $blog_id ), + $this->equalTo( 'testsite' ), + $this->equalTo( 'deleted' ), + ), + array( + $this->equalTo( + _x( + '"%1$s" domain alias was make primary for "%2$s"', + '1. Domain alias 2. Site name', + 'stream' + ) + ), + $this->equalTo( + array( + 'domain' => 'example.com', + 'site_name' => 'testsite', + ) + ), + $this->equalTo( $blog_id ), + $this->equalTo( 'testsite' ), + $this->equalTo( 'made_primary' ), + ) + ); + + // Make alias primary domain for site to trigger callback. + $mapping->make_primary(); + + // Check callback test action. + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_mercator_mapping_made_primary' ) ); + } + + public function test_callback_mercator_mapping_updated() { + // Make blog and aliases for later use. + $blog_id = $this->factory->blog->create( array( 'title' => 'testsite' ) ); + $mapping = \Mercator\Mapping::create( $blog_id, 'example.com', true ); + $mapping->make_primary(); + + // Expected log calls. + $this->mock->expects( $this->once() ) + ->method( 'log' ) + ->with( + $this->equalTo( + _x( + 'The domain alias "%1$s" was updated to "%2$s" for site "%3$s"', + '1. Old Domain alias 2. Domain alias 2. Site name', + 'stream' + ) + ), + $this->equalTo( + array( + 'old_domain' => 'example.com', + 'domain' => 'testsite.com', + 'site_name' => 'testsite', + ) + ), + $this->equalTo( $blog_id ), + $this->equalTo( 'testsite' ), + $this->equalTo( 'updated' ) + ); + + // Change site alias to trigger callback. + $mapping->set_domain( 'testsite.com' ); + + // Check callback test action. + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_mercator_mapping_updated' ) ); + } + + public function test_callback_mercator_mapping_deleted() { + // Make blog and alias for later use. + $blog_id = $this->factory->blog->create( array( 'title' => 'testsite' ) ); + $mapping = \Mercator\Mapping::create( $blog_id, 'example.com', true ); + + // Expected log calls. + $this->mock->expects( $this->once() ) + ->method( 'log' ) + ->with( + $this->equalTo( + _x( + '"%1$s" domain alias was deleted for "%2$s"', + '1. Domain alias 2. Site name', + 'stream' + ) + ), + $this->equalTo( + array( + 'domain' => 'example.com', + 'site_name' => 'testsite', + ) + ), + $this->equalTo( $blog_id ), + $this->equalTo( 'testsite' ), + $this->equalTo( 'deleted' ) + ); + + // Execute action to trigger callback because the tables need to + // run the "\Mercator\Mapping::delete() don't currently exist. + do_action( 'mercator.mapping.deleted', $mapping ); + + // Check callback test action. + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_mercator_mapping_deleted' ) ); + } + + public function test_callback_mercator_mapping_created() { + // Make blog for later use. + $blog_id = $this->factory->blog->create( array( 'title' => 'testsite' ) ); + + // Expected log calls. + $this->mock->expects( $this->once() ) + ->method( 'log' ) + ->with( + $this->equalTo( + _x( + '"%1$s" domain alias was created for "%2$s"', + '1. Domain alias 2. Site name', + 'stream' + ) + ), + $this->equalTo( + array( + 'domain' => 'example.com', + 'site_name' => 'testsite', + ) + ), + $this->equalTo( $blog_id ), + $this->equalTo( 'testsite' ), + $this->equalTo( 'created' ) + ); + + // Create and assign domain alias to site to trigger callback. + \Mercator\Mapping::create( $blog_id, 'example.com', true ); + + // Check callback test action. + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_mercator_mapping_created' ) ); + } }