Changeset 7 in tests
- Timestamp:
- 09/20/2007 01:07:55 AM (19 years ago)
- File:
-
- 1 edited
-
wp-testlib/base.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
wp-testlib/base.php
r1 r7 45 45 query_posts($path); 46 46 } 47 48 // various helper functions for creating and deleting posts, pages etc 49 50 // as it suggests: delete all posts and pages 51 function _delete_all_posts() { 52 global $wpdb; 53 54 $all_posts = $wpdb->get_col("SELECT ID from {$wpdb->posts}"); 55 foreach ($all_posts as $id) 56 wp_delete_post($id); 57 } 58 59 // insert a given number of trivial posts, each with predictable title, content and excerpt 60 function _insert_quick_posts($num, $type='post') { 61 for ($i=0; $i<$num; $i++) 62 $this->post_ids[] = wp_insert_post(array( 63 'post_author' => $this->author->ID, 64 'post_status' => 'publish', 65 'post_title' => "{$type} title {$i}", 66 'post_content' => "{$type} content {$i}", 67 'post_excerpt' => "{$type} excerpt {$i}", 68 )); 69 } 70 71 // insert a given number of trivial pages, each with predictable title, content and excerpt 72 function _insert_quick_pages($num) { 73 $this->_insert_quick_posts($num, 'page'); 74 } 75 76 // import a WXR file 77 function _import_wp($filename) { 78 $importer = new WP_Import(); 79 $path = realpath($filename); 80 var_dump(__FUNCTION__, $filename, $path); 81 assert('!empty($path)'); 82 assert('is_file($path)'); 83 84 // this is copied from WP_Import::import() 85 // we can't call that function directly because it expects a file ID 86 $importer->file = realpath($filename); 87 $importer->get_authors_from_post(); 88 $importer->get_entries(); 89 $importer->process_categories(); 90 $importer->process_posts(); 91 } 92 93 // Generate PHP source code containing unit tests for the current blog contents. 94 // When run, the tests will check that the content of the blog exactly matches what it is now, 95 // with a separate test function for each post. 96 function _generate_post_content_test(&$posts) { 97 $out = ''; 98 foreach ($posts as $i=>$post) { 99 $out .= "\n\tfunction test_post_{$i}() {\n"; 100 $out .= "\t\t\$post = \$this->posts[{$i}];\n"; 101 foreach (array_keys(get_object_vars($post)) as $field) { 102 if ($field != 'ID') 103 $out .= "\t\t".'$this->assertEqual($post->'.$field.', \''.addcslashes($post->$field, "\n\r\t'\\").'\');'."\n"; 104 } 105 $cat_ids = wp_get_post_categories($post->ID); 106 $out .= "\t\t".'$cats = wp_get_post_categories($post->ID);'."\n"; 107 $out .= "\t\t".'$this->assertEqual(count($cats), '.count($cat_ids).');'."\n"; 108 if ($cat_ids) { 109 foreach ($cat_ids as $j=>$cat_id) 110 $out .= "\t\t".'$this->assertEqual(get_cat_name($cats['.$j.']), \''.addslashes(get_cat_name($cat_id)).'\');'."\n"; 111 } 112 $out .= "\t}\n\n"; 113 } 114 115 return $out; 116 } 117 118 47 119 } 48 120
Note: See TracChangeset
for help on using the changeset viewer.