diff --git a/.gitignore b/.gitignore index 6101c86..7bb709d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ node_modules/ *.tar.gz *.zip vendor/ +.phpunit.result.cache diff --git a/README.md b/README.md index 340fc9e..c6ead29 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,24 @@ define( 'GEOLONIA_GIS_DEFAULT_ZOOM', 16 ); define( 'GEOLONIA_GIS_DEFAULT_LAT', 34.86707002642607 ); define( 'GEOLONIA_GIS_DEFAULT_LNG', 138.32283481501884 ); ``` + + +## 開発者向け + +### テスト環境のセットアップ + +- 初回のみ以下のコマンドを実行して依存関係のインストールと、テスト用の WordPress のセットアップを行ってください。 +- `svn` と `php` のインストールが必要です + +```bash +$ composer install +$ bash bin/install-wp-tests.sh wordpress_test root '' localhost latest +``` + +### テストの実行 + +テストを実行するためには、以下のコマンドを実行してください。 + +```bash +$ composer test +``` diff --git a/composer.json b/composer.json index e597722..51d1c82 100644 --- a/composer.json +++ b/composer.json @@ -3,5 +3,8 @@ "phpunit/phpunit": "^8.0", "yoast/phpunit-polyfills": "^2.0", "doctrine/instantiator": "^1.4" + }, + "scripts": { + "test": "phpunit" } } diff --git a/geolonia-open-gis.php b/geolonia-open-gis.php index 14a80a4..ede9661 100644 --- a/geolonia-open-gis.php +++ b/geolonia-open-gis.php @@ -42,13 +42,10 @@ require_once( dirname( __FILE__) . '/inc/functions.php' ); -add_action( 'plugins_loaded', function() { - load_plugin_textdomain( - "geolonia-open-gis", - false, - dirname( plugin_basename( __FILE__ ) ).'/languages' - ); -} ); +function geolonia_gis_load_textdomain() { + return load_plugin_textdomain( 'geolonia-open-gis', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); +} +add_action( 'plugins_loaded', 'geolonia_gis_load_textdomain'); // Registers the custom post type `maps`. add_action( 'init', function() { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 50421ec..e8e8560 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,7 +10,6 @@ ./tests/ - ./tests/test-sample.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 78a7b42..1d38831 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -5,6 +5,10 @@ * @package Geolonia_Gis */ + +// WP_PLUGIN_DIR 現在のプラグインの上位ディレクトリを指定する +define('WP_PLUGIN_DIR', dirname(dirname(dirname(__FILE__)))); + // Load PHPUnit Polyfills. require_once dirname( __FILE__ ) . '/../vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php'; diff --git a/tests/test-load-textdomain.php b/tests/test-load-textdomain.php new file mode 100644 index 0000000..d2f524a --- /dev/null +++ b/tests/test-load-textdomain.php @@ -0,0 +1,73 @@ +user->create( + array( + 'role' => 'administrator', + 'locale' => 'ja', + ) + ); + } + + /** + * @covers ::geolonia_gis_load_textdomain + */ + public function test_load_textdomain() { + + add_filter('locale', function() { + return 'ja'; + }); + $this->assertTrue( geolonia_gis_load_textdomain() ); + } + + /** + * @covers ::geolonia_gis_load_textdomain + */ + public function test_load_textdomain_by_plugin_locale() { + add_filter('plugin_locale', function() { + return 'ja'; + }); + $this->assertTrue( geolonia_gis_load_textdomain() ); + } + + /** + * @covers ::geolonia_gis_load_textdomain + */ + public function test_load_textdomain_when_locale_is_english() { + add_filter('locale', function() { + return 'en_US'; + }); + $this->assertFalse( geolonia_gis_load_textdomain() ); + } + + /** + * @covers ::geolonia_gis_load_textdomain + */ + public function test_load_textdomain_plugin_locale_is_english() { + add_filter('plugin_locale', function() { + return 'en_US'; + }); + $this->assertFalse( geolonia_gis_load_textdomain() ); + } + + /** + * @covers ::geolonia_gis_load_textdomain + */ + public function test_load_textdomain_user_locale_is_japanese() { + + add_filter('locale', function() { + return 'en_US'; + }); + + set_current_screen( 'dashboard' ); + wp_set_current_user( self::$user_id ); + + $this->assertTrue( geolonia_gis_load_textdomain() ); + } +} diff --git a/tests/test-register_post_type_maps.php b/tests/test-register_post_type_maps.php new file mode 100644 index 0000000..d24fde9 --- /dev/null +++ b/tests/test-register_post_type_maps.php @@ -0,0 +1,94 @@ +assertNull( get_post_type_object( 'maps' ) ); + + register_post_type_maps(); + + $maps_object = get_post_type_object( 'maps' ); + + $this->assertInstanceOf( 'WP_Post_Type', $maps_object ); + $this->assertSame( 'maps', $maps_object->name ); + } + + /** + * @covers ::register_post_type_maps + */ + public function test_maps_post_type_options() { + + $this->assertNull( get_post_type_object( 'maps' ) ); + + register_post_type_maps(); + + $maps_object = get_post_type_object( 'maps' ); + + // labels の設定値を確認 + $this->assertSame( 'Map', $maps_object->label ); + + // public の設定値を確認 + $this->assertTrue( $maps_object->public ); + + // hierarchical の設定値を確認 + $this->assertFalse( $maps_object->hierarchical ); + + // show_ui の設定値を確認 + $this->assertTrue( $maps_object->show_ui ); + + // show_in_menu の設定値を確認 + $this->assertTrue( $maps_object->show_in_nav_menus ); + + // カスタム投稿タイプの supports に title が含まれているか確認 + $this->assertTrue( post_type_supports( 'maps', 'title' ), 'Title support is enabled' ); + + // カスタム投稿タイプの supports に editor が含まれているか確認 + $this->assertTrue( post_type_supports( 'maps', 'editor' ), 'Editor support is enabled' ); + + // カスタム投稿タイプの supports に author が含まれているか確認 + $this->assertTrue( post_type_supports( 'maps', 'revisions' ), 'Revisions support is enabled' ); + + // カスタム投稿タイプの supports に excerpt が含まれているか確認 + $this->assertTrue( post_type_supports( 'maps', 'excerpt' ), 'Excerpt support is enabled' ); + + // archive の設定値を確認 + $this->assertFalse( $maps_object->has_archive ); + + // rewrite の設定値を確認 + $this->assertSame( array('slug' => 'maps'), $maps_object->rewrite ); + + // query_var の設定値を確認 + $this->assertSame( 'maps', $maps_object->query_var ); + + // menu_icon の設定値を確認 + $this->assertSame( 'dashicons-location', $maps_object->menu_icon ); + + // show_in_rest の設定値を確認 + $this->assertTrue( $maps_object->show_in_rest ); + + // rest_base の設定値を確認 + $this->assertSame( GEOLONIA_GIS_POST_TYPE, $maps_object->rest_base ); + + // rest_controller_class の設定値を確認 + $this->assertSame( 'WP_REST_Posts_Controller', $maps_object->rest_controller_class ); + + // taxonomies の設定値を確認 + $this->assertSame( array( 'map_tag' ), $maps_object->taxonomies ); + + // capability_type の設定値を確認 + $this->assertSame( 'map', $maps_object->capability_type ); + + // map_meta_cap の設定値を確認 + $this->assertTrue( $maps_object->map_meta_cap ); + } +} diff --git a/tests/test-sample.php b/tests/test-sample.php deleted file mode 100644 index 3fd328b..0000000 --- a/tests/test-sample.php +++ /dev/null @@ -1,20 +0,0 @@ -assertTrue( true ); - } -}