PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/import/import.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests
PHP | 226 lines | 189 code | 29 blank | 8 comment | 2 complexity | 8b8ef10889454785eb7011804c2d38a1 MD5 | raw file
  1. <?php
  2. /**
  3. * @group import
  4. */
  5. class Tests_Import_Import extends WP_Import_UnitTestCase {
  6. function setUp() {
  7. parent::setUp();
  8. if ( ! defined( 'WP_IMPORTING' ) )
  9. define( 'WP_IMPORTING', true );
  10. if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
  11. define( 'WP_LOAD_IMPORTERS', true );
  12. add_filter( 'import_allow_create_users', '__return_true' );
  13. require_once DIR_TESTDATA . '/plugins/wordpress-importer/wordpress-importer.php';
  14. global $wpdb;
  15. // crude but effective: make sure there's no residual data in the main tables
  16. foreach ( array('posts', 'postmeta', 'comments', 'terms', 'term_taxonomy', 'term_relationships', 'users', 'usermeta') as $table)
  17. $wpdb->query("DELETE FROM {$wpdb->$table}");
  18. }
  19. function tearDown() {
  20. remove_filter( 'import_allow_create_users', '__return_true' );
  21. parent::tearDown();
  22. }
  23. function test_small_import() {
  24. global $wpdb;
  25. $authors = array( 'admin' => false, 'editor' => false, 'author' => false );
  26. $this->_import_wp( DIR_TESTDATA . '/export/small-export.xml', $authors );
  27. // ensure that authors were imported correctly
  28. $user_count = count_users();
  29. $this->assertEquals( 3, $user_count['total_users'] );
  30. $admin = get_user_by( 'login', 'admin' );
  31. $this->assertEquals( 'admin', $admin->user_login );
  32. $this->assertEquals( 'local@host.null', $admin->user_email );
  33. $editor = get_user_by( 'login', 'editor' );
  34. $this->assertEquals( 'editor', $editor->user_login );
  35. $this->assertEquals( 'editor@example.org', $editor->user_email );
  36. $this->assertEquals( 'FirstName', $editor->user_firstname );
  37. $this->assertEquals( 'LastName', $editor->user_lastname );
  38. $author = get_user_by( 'login', 'author' );
  39. $this->assertEquals( 'author', $author->user_login );
  40. $this->assertEquals( 'author@example.org', $author->user_email );
  41. // check that terms were imported correctly
  42. $this->assertEquals( 30, wp_count_terms( 'category' ) );
  43. $this->assertEquals( 3, wp_count_terms( 'post_tag' ) );
  44. $foo = get_term_by( 'slug', 'foo', 'category' );
  45. $this->assertEquals( 0, $foo->parent );
  46. $bar = get_term_by( 'slug', 'bar', 'category' );
  47. $foo_bar = get_term_by( 'slug', 'foo-bar', 'category' );
  48. $this->assertEquals( $bar->term_id, $foo_bar->parent );
  49. // check that posts/pages were imported correctly
  50. $post_count = wp_count_posts( 'post' );
  51. $this->assertEquals( 5, $post_count->publish );
  52. $this->assertEquals( 1, $post_count->private );
  53. $page_count = wp_count_posts( 'page' );
  54. $this->assertEquals( 4, $page_count->publish );
  55. $this->assertEquals( 1, $page_count->draft );
  56. $comment_count = wp_count_comments();
  57. $this->assertEquals( 1, $comment_count->total_comments );
  58. $posts = get_posts( array( 'numberposts' => 20, 'post_type' => 'any', 'post_status' => 'any', 'orderby' => 'ID' ) );
  59. $this->assertEquals( 11, count($posts) );
  60. $post = $posts[0];
  61. $this->assertEquals( 'Many Categories', $post->post_title );
  62. $this->assertEquals( 'many-categories', $post->post_name );
  63. $this->assertEquals( $admin->ID, $post->post_author );
  64. $this->assertEquals( 'post', $post->post_type );
  65. $this->assertEquals( 'publish', $post->post_status );
  66. $this->assertEquals( 0, $post->post_parent );
  67. $cats = wp_get_post_categories( $post->ID );
  68. $this->assertEquals( 27, count($cats) );
  69. $post = $posts[1];
  70. $this->assertEquals( 'Non-standard post format', $post->post_title );
  71. $this->assertEquals( 'non-standard-post-format', $post->post_name );
  72. $this->assertEquals( $admin->ID, $post->post_author );
  73. $this->assertEquals( 'post', $post->post_type );
  74. $this->assertEquals( 'publish', $post->post_status );
  75. $this->assertEquals( 0, $post->post_parent );
  76. $cats = wp_get_post_categories( $post->ID );
  77. $this->assertEquals( 1, count($cats) );
  78. $this->assertTrue( has_post_format( 'aside', $post->ID ) );
  79. $post = $posts[2];
  80. $this->assertEquals( 'Top-level Foo', $post->post_title );
  81. $this->assertEquals( 'top-level-foo', $post->post_name );
  82. $this->assertEquals( $admin->ID, $post->post_author );
  83. $this->assertEquals( 'post', $post->post_type );
  84. $this->assertEquals( 'publish', $post->post_status );
  85. $this->assertEquals( 0, $post->post_parent );
  86. $cats = wp_get_post_categories( $post->ID, array( 'fields' => 'all' ) );
  87. $this->assertEquals( 1, count($cats) );
  88. $this->assertEquals( 'foo', $cats[0]->slug );
  89. $post = $posts[3];
  90. $this->assertEquals( 'Foo-child', $post->post_title );
  91. $this->assertEquals( 'foo-child', $post->post_name );
  92. $this->assertEquals( $editor->ID, $post->post_author );
  93. $this->assertEquals( 'post', $post->post_type );
  94. $this->assertEquals( 'publish', $post->post_status );
  95. $this->assertEquals( 0, $post->post_parent );
  96. $cats = wp_get_post_categories( $post->ID, array( 'fields' => 'all' ) );
  97. $this->assertEquals( 1, count($cats) );
  98. $this->assertEquals( 'foo-bar', $cats[0]->slug );
  99. $post = $posts[4];
  100. $this->assertEquals( 'Private Post', $post->post_title );
  101. $this->assertEquals( 'private-post', $post->post_name );
  102. $this->assertEquals( $admin->ID, $post->post_author );
  103. $this->assertEquals( 'post', $post->post_type );
  104. $this->assertEquals( 'private', $post->post_status );
  105. $this->assertEquals( 0, $post->post_parent );
  106. $cats = wp_get_post_categories( $post->ID );
  107. $this->assertEquals( 1, count($cats) );
  108. $tags = wp_get_post_tags( $post->ID );
  109. $this->assertEquals( 3, count($tags) );
  110. $this->assertEquals( 'tag1', $tags[0]->slug );
  111. $this->assertEquals( 'tag2', $tags[1]->slug );
  112. $this->assertEquals( 'tag3', $tags[2]->slug );
  113. $post = $posts[5];
  114. $this->assertEquals( '1-col page', $post->post_title );
  115. $this->assertEquals( '1-col-page', $post->post_name );
  116. $this->assertEquals( $admin->ID, $post->post_author );
  117. $this->assertEquals( 'page', $post->post_type );
  118. $this->assertEquals( 'publish', $post->post_status );
  119. $this->assertEquals( 0, $post->post_parent );
  120. $this->assertEquals( 'onecolumn-page.php', get_post_meta( $post->ID, '_wp_page_template', true ) );
  121. $post = $posts[6];
  122. $this->assertEquals( 'Draft Page', $post->post_title );
  123. $this->assertEquals( '', $post->post_name );
  124. $this->assertEquals( $admin->ID, $post->post_author );
  125. $this->assertEquals( 'page', $post->post_type );
  126. $this->assertEquals( 'draft', $post->post_status );
  127. $this->assertEquals( 0, $post->post_parent );
  128. $this->assertEquals( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
  129. $post = $posts[7];
  130. $this->assertEquals( 'Parent Page', $post->post_title );
  131. $this->assertEquals( 'parent-page', $post->post_name );
  132. $this->assertEquals( $admin->ID, $post->post_author );
  133. $this->assertEquals( 'page', $post->post_type );
  134. $this->assertEquals( 'publish', $post->post_status );
  135. $this->assertEquals( 0, $post->post_parent );
  136. $this->assertEquals( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
  137. $post = $posts[8];
  138. $this->assertEquals( 'Child Page', $post->post_title );
  139. $this->assertEquals( 'child-page', $post->post_name );
  140. $this->assertEquals( $admin->ID, $post->post_author );
  141. $this->assertEquals( 'page', $post->post_type );
  142. $this->assertEquals( 'publish', $post->post_status );
  143. $this->assertEquals( $posts[7]->ID, $post->post_parent );
  144. $this->assertEquals( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
  145. $post = $posts[9];
  146. $this->assertEquals( 'Sample Page', $post->post_title );
  147. $this->assertEquals( 'sample-page', $post->post_name );
  148. $this->assertEquals( $admin->ID, $post->post_author );
  149. $this->assertEquals( 'page', $post->post_type );
  150. $this->assertEquals( 'publish', $post->post_status );
  151. $this->assertEquals( 0, $post->post_parent );
  152. $this->assertEquals( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
  153. $post = $posts[10];
  154. $this->assertEquals( 'Hello world!', $post->post_title );
  155. $this->assertEquals( 'hello-world', $post->post_name );
  156. $this->assertEquals( $author->ID, $post->post_author );
  157. $this->assertEquals( 'post', $post->post_type );
  158. $this->assertEquals( 'publish', $post->post_status );
  159. $this->assertEquals( 0, $post->post_parent );
  160. $cats = wp_get_post_categories( $post->ID );
  161. $this->assertEquals( 1, count($cats) );
  162. }
  163. function test_double_import() {
  164. $authors = array( 'admin' => false, 'editor' => false, 'author' => false );
  165. $this->_import_wp( DIR_TESTDATA . '/export/small-export.xml', $authors );
  166. $this->_import_wp( DIR_TESTDATA . '/export/small-export.xml', $authors );
  167. $user_count = count_users();
  168. $this->assertEquals( 3, $user_count['total_users'] );
  169. $admin = get_user_by( 'login', 'admin' );
  170. $this->assertEquals( 'admin', $admin->user_login );
  171. $this->assertEquals( 'local@host.null', $admin->user_email );
  172. $editor = get_user_by( 'login', 'editor' );
  173. $this->assertEquals( 'editor', $editor->user_login );
  174. $this->assertEquals( 'editor@example.org', $editor->user_email );
  175. $this->assertEquals( 'FirstName', $editor->user_firstname );
  176. $this->assertEquals( 'LastName', $editor->user_lastname );
  177. $author = get_user_by( 'login', 'author' );
  178. $this->assertEquals( 'author', $author->user_login );
  179. $this->assertEquals( 'author@example.org', $author->user_email );
  180. $this->assertEquals( 30, wp_count_terms( 'category' ) );
  181. $this->assertEquals( 3, wp_count_terms( 'post_tag' ) );
  182. $foo = get_term_by( 'slug', 'foo', 'category' );
  183. $this->assertEquals( 0, $foo->parent );
  184. $bar = get_term_by( 'slug', 'bar', 'category' );
  185. $foo_bar = get_term_by( 'slug', 'foo-bar', 'category' );
  186. $this->assertEquals( $bar->term_id, $foo_bar->parent );
  187. $post_count = wp_count_posts( 'post' );
  188. $this->assertEquals( 5, $post_count->publish );
  189. $this->assertEquals( 1, $post_count->private );
  190. $page_count = wp_count_posts( 'page' );
  191. $this->assertEquals( 4, $page_count->publish );
  192. $this->assertEquals( 1, $page_count->draft );
  193. $comment_count = wp_count_comments();
  194. $this->assertEquals( 1, $comment_count->total_comments );
  195. }
  196. // function test_menu_import
  197. }