Skip to content

Commit

Permalink
Merge pull request #3 from naogify/test-register-post-type
Browse files Browse the repository at this point in the history
geolonia_gis_load_textdomain と register_post_type_maps にユニットテストを追加
  • Loading branch information
miya0001 authored Jul 5, 2024
2 parents 8e4a5f4 + b86864c commit 7702c6f
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules/
*.tar.gz
*.zip
vendor/
.phpunit.result.cache
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"phpunit/phpunit": "^8.0",
"yoast/phpunit-polyfills": "^2.0",
"doctrine/instantiator": "^1.4"
},
"scripts": {
"test": "phpunit"
}
}
11 changes: 4 additions & 7 deletions geolonia-open-gis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<testsuites>
<testsuite name="testing">
<directory prefix="test-" suffix=".php">./tests/</directory>
<exclude>./tests/test-sample.php</exclude>
</testsuite>
</testsuites>
</phpunit>
4 changes: 4 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
73 changes: 73 additions & 0 deletions tests/test-load-textdomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Class TestLoadTextdomain
*/

class Test_LoadTextdomain extends WP_UnitTestCase {
protected static $user_id;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$user_id = $factory->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() );
}
}
94 changes: 94 additions & 0 deletions tests/test-register_post_type_maps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

class Test_Register_Post_Type_Maps extends WP_UnitTestCase {

public function set_up() {
parent::set_up();

unregister_post_type( 'maps' );
}

/**
* @covers ::register_post_type_maps
*/
public function test_maps_post_type_registered() {

$this->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 );
}
}
20 changes: 0 additions & 20 deletions tests/test-sample.php

This file was deleted.

0 comments on commit 7702c6f

Please sign in to comment.