2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WordPressテーマユニットテストデータで確認してまた戻す

Last updated at Posted at 2017-11-20

WordFesNagoyaに参加して「プログラミング初心者こそ取り組もう!テストファーストで作るこれからのテーマ・プラグイン開発」でテストを簡単に回せるようにしたいなーと思って、テーマユニットテストデータのインポートを楽にできるようにしてみました。(やっていた内容は違うけど・・・)
サイト用のすべてのデータを作ってbehatで表示確認・・・って回せたらサイトの確認とか楽ちんになりそうですね。

投稿の表示を確認するためにテーマユニットテストデータをインポートして確認してそのままだと納品できないのでデータを元のデータに戻します。
gistに登録してエイリアスから実行します。

dbをexportしてからテストデータのインポート

テーマユニットテスト

vagrant ssh -c "mkdir /vagrant/tmp && wp db export /vagrant/tmp/local-`date +%Y%m%d%H%M`.sql || wp db export /vagrant/tmp/local-`date +%Y%m%d%H%M`.sql ; wget https://raw.githubusercontent.com/jawordpressorg/theme-test-data-ja/master/wordpress-theme-test-date-ja.xml --no-check-certificate  && wp plugin install wordpress-importer --activate && wp import wordpress-theme-test-date-ja.xml --authors=create && rm wordpress-theme-test-date-ja.xml"
vagrant ssh -c "mkdir /vagrant/tmp && wp db export /vagrant/tmp/unittest.sql || wp db export /vagrant/tmp/unittest.sql ; wget https://raw.githubusercontent.com/jawordpressorg/theme-test-data-ja/master/wordpress-theme-test-date-ja.xml --no-check-certificate  && wp plugin install wordpress-importer --activate && wp import wordpress-theme-test-date-ja.xml --authors=create && rm wordpress-theme-test-date-ja.xml"

※reset時のgawkがうまくいってなかったので固定のファイル名に変更

shellscriptの書き方がいまいちわかってないので指摘まってます。
テーマユニットテストデータをimportする前にsqlをexport してます。
※/vagrant/tmpが存在しているときはもう一回 wp db export してます。ワンライナーでの条件分岐の書き方がよくわからなかったので・・・

.bash_profileに登録してtestdataと実行するだけでインポートされるようにします。

alias testdata='curl -L https://gist.githubusercontent.com/GotoKazu/d8adf93c293bdd348baeaa91d1259c39/raw/9b43afd07d39eb5cfe70897561df1b469ab52f9e/theme_test_data_unittest_sql.sh | bash'
source ~/.bash_profile

vccwのVagrantfileのディレクトリで実行するとインポートされます。

[WordPress] WP-CLIでテストデータをインポートする方法

テストデータを元に戻す

テストデータをインポートしたままではまずいのでテストデータをimportした際のデータ(tmpディレクトリ内の一番新しいsqlファイル)に戻したいです。

vagrant ssh -c "wp db reset --yes | wp db import - | ls -lt /vagrant/tmp/*.sql | head -n 1 | gawk '{print $9}'"

vagrant ssh -c "wp db reset --yes ; wp db import /vagrant/tmp/unittest.sql"

sqlをexportした時にlocal-[日時].sqlとしてますがtestdata.sqlなどにすれば読み込むときもそのまま指定できると思います。

こっちもaliasに追加してそのまま実行できるようにします。

alias testreset='curl -L https://gist.githubusercontent.com/GotoKazu/d8adf93c293bdd348baeaa91d1259c39/raw/9b43afd07d39eb5cfe70897561df1b469ab52f9e/theme_test_data_unittest_sql.sh | bash'

※gawkのところがうまくいってなかったのでファイル名を固定に変更。そのうちなおす。

source ~/.bash_profile

シェルスクリプトで最新のファイル名を取得する

vccwを構築する際にそのまま設定することも・・・

vccw/site.yml

#
# Theme unit testing
#
theme_unit_test: true
# theme_unit_test_uri: https://wpcom-themes.svn.automattic.com/demo/theme-unit-test-data.xml
theme_unit_test_uri: https://raw.githubusercontent.com/jawordpressorg/theme-test-data-ja/master/wordpress-theme-test-date-ja.xml
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?