post-fixtures/test/PostFixturesTest.php

433 lines
10 KiB
PHP
Raw Normal View History

2009-11-22 20:35:39 +00:00
<?php
2009-11-22 20:50:16 +00:00
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
2010-01-18 02:28:26 +00:00
require_once('vfsStream/vfsStream.php');
2009-11-22 20:50:16 +00:00
require_once(dirname(__FILE__) . '/../classes/PostFixtures.inc');
class PostFixturesTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$this->pf = new PostFixtures();
2010-01-14 21:19:58 +00:00
$_POST = array();
2010-01-18 02:28:26 +00:00
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
2010-01-14 21:19:58 +00:00
}
function tearDown() {
$_POST = array();
2009-11-22 20:50:16 +00:00
}
function providerTestParseJSON() {
return array(
array(false, false),
array('', false),
array('{]', false),
array('{}', array()),
2009-11-22 20:56:00 +00:00
array('[]', array()),
array('["test", "test2"]', array('test', 'test2')),
2010-01-14 20:02:52 +00:00
array('{"one": "test", "two": "test2"}', array('one' => 'test', 'two' => 'test2'))
2009-11-22 20:50:16 +00:00
);
}
/**
* @dataProvider providerTestParseJSON
*/
function testParseJSON($input, $expected_output) {
$this->assertEquals($expected_output, $this->pf->parse_json($input));
}
2009-11-22 22:34:57 +00:00
function testRemoveAllPosts() {
global $wp_test_expectations;
$posts = array();
for ($i = 0; $i < 5; ++$i) {
$post = (object)array('ID' => $i);
wp_insert_post($post);
$posts[] = $post;
update_post_meta($i, md5(rand()), md5(rand()));
}
2009-11-23 00:57:35 +00:00
_set_up_get_posts_response(array('numberposts' => '-1', 'post_status' => 'draft,pending,future,inherit,private,publish'), $posts);
2009-11-22 22:34:57 +00:00
$this->assertEquals(5, count($wp_test_expectations['posts']));
$this->assertEquals(5, count($wp_test_expectations['post_meta']));
$this->pf->remove_all_posts();
$this->assertEquals(0, count($wp_test_expectations['posts']));
$this->assertEquals(0, count($wp_test_expectations['post_meta']));
}
2009-11-22 22:58:34 +00:00
function testRemoveAllCategories() {
global $wp_test_expectations;
update_option('default_category', 0);
for ($i = 0; $i < 5; ++$i) {
add_category($i, (object)array('slug' => 'test-' . $i));
}
$this->assertEquals(5, count($wp_test_expectations['categories']));
$this->pf->remove_all_categories();
$this->assertEquals(1, count($wp_test_expectations['categories']));
$result = get_category(0);
$this->assertTrue(isset($result->term_id));
}
2009-11-22 23:11:59 +00:00
function providerTestProcessData() {
return array(
array(
array(
2009-11-23 13:16:55 +00:00
'posts' => array(
array(
'title' => 'test',
'categories' => array('test1', 'test2'),
'date' => '2009-01-01',
'metadata' => array(
'test' => 'test2'
)
2009-11-22 23:11:59 +00:00
)
)
),
array(
'posts' => array(
array(
'title' => 'test',
'categories' => array('test1', 'test2'),
'date' => '2009-01-01',
'metadata' => array(
'test' => 'test2'
)
)
),
2009-11-23 13:18:59 +00:00
'categories' => array('test1', 'test2'),
'options' => array()
2009-11-22 23:11:59 +00:00
)
),
2009-11-23 13:18:59 +00:00
array(
array(
'options' => array(
'test' => 'test2'
)
),
array(
'posts' => array(),
'categories' => array(),
'options' => array(
'test' => 'test2'
)
)
)
2009-11-22 23:11:59 +00:00
);
}
/**
* @dataProvider providerTestProcessData
*/
function testProcessData($data, $expected_output) {
$this->assertEquals($expected_output, $this->pf->process_data($data));
}
2009-11-22 23:32:52 +00:00
function providerTestCreateCategories() {
return array(
array(false, array()),
array(array(), array()),
2009-11-23 12:23:26 +00:00
array(array('test'), array('test' => 1)),
array(array('test/test2'), array('test' => 1, 'test/test2' => 2)),
2009-11-22 23:32:52 +00:00
);
}
/**
* @dataProvider providerTestCreateCategories
*/
function testCreateCategories($input, $expected_output) {
$this->assertEquals($expected_output, $this->pf->create_categories($input));
}
2009-11-22 23:57:23 +00:00
function providerTestCreatePosts() {
return array(
array(false, array(), false),
array(
array(),
array()
),
array(
array(
array(
'post_title' => 'test'
)
),
array(1),
2010-01-18 04:56:41 +00:00
array(
'categories' => array(
1 => array(1)
)
)
2009-11-22 23:57:23 +00:00
),
array(
array(
array(
'post_title' => 'test',
'categories' => array('test2')
)
),
array(1),
2010-01-18 04:56:41 +00:00
array(
'categories' => array(
1 => array(2)
)
)
2009-11-22 23:57:23 +00:00
),
array(
array(
array(
'post_title' => 'test',
'categories' => array('test2'),
'metadata' => array(
'test' => 'test2'
2010-01-18 04:56:41 +00:00
),
'tags' => array(
'tag1', 'tag2'
2009-11-22 23:57:23 +00:00
)
)
),
array(1),
2010-01-18 04:56:41 +00:00
array(
'categories' => array(
1 => array(2)
),
'metadata' => array(
1 => array('test' => 'test2')
),
'tags' => array(
1 => array('tag1', 'tag2')
)
)
2009-11-22 23:57:23 +00:00
),
2009-11-23 12:23:26 +00:00
array(
array(
array(
'post_title' => 'test',
'categories' => array('test', 'test2/test3')
)
),
array(1),
2010-01-18 04:56:41 +00:00
array(
'categories' => array(
1 => array(1,3)
)
)
2009-11-23 12:23:26 +00:00
),
2009-11-22 23:57:23 +00:00
);
}
/**
* @dataProvider providerTestCreatePosts
*/
2010-01-18 04:56:41 +00:00
function testCreatePosts($posts, $expected_post_ids, $expected_additional_info = false) {
global $wp_test_expectations;
2009-11-22 23:57:23 +00:00
update_option('default_category', 1);
wp_insert_category(array('slug' => 'test'));
2009-11-23 12:23:26 +00:00
$this->assertEquals($expected_post_ids, $this->pf->create_posts($posts, array('test' => 1, 'test2' => 2, 'test2/test3' => 3)));
2009-11-22 23:57:23 +00:00
2010-01-18 04:56:41 +00:00
if (is_array($expected_additional_info)) {
extract($expected_additional_info);
if (isset($categories)) {
if (is_array($categories)) {
foreach ($categories as $post_id => $cats) {
$this->assertEquals($cats, wp_get_post_categories($post_id));
}
}
2009-11-22 23:57:23 +00:00
}
2010-01-18 04:56:41 +00:00
if (isset($metadata)) {
if (is_array($metadata)) {
foreach ($metadata as $post_id => $metadata_info) {
foreach ($metadata_info as $key => $value) {
$this->assertEquals($value, get_post_meta($post_id, $key, true));
}
}
}
}
if (isset($tags)) {
if (is_array($tags)) {
foreach ($tags as $post_id => $expected_tags) {
$post_tags = wp_get_post_tags($post_id);
foreach ($post_tags as &$tag) {
$tag = $tag->slug;
}
$this->assertEquals($post_tags, $expected_tags);
}
2009-11-22 23:57:23 +00:00
}
}
}
}
2009-11-23 00:05:20 +00:00
function testCreate() {
2009-11-23 13:20:52 +00:00
$pf = $this->getMock('PostFixtures', array('create_posts', 'create_categories', 'process_blog_options'));
$pf->expects($this->once())->method('create_posts')->with('posts', 'processed');
$pf->expects($this->once())->method('create_categories')->with('categories')->will($this->returnValue('processed'));
$pf->expects($this->once())->method('process_blog_options')->with('options', 'processed');
2009-11-23 00:05:20 +00:00
2009-11-23 13:20:52 +00:00
$pf->create(array('posts' => 'posts', 'categories' => 'categories', 'options' => 'options'));
2009-11-23 00:05:20 +00:00
}
2009-11-23 00:07:13 +00:00
function testRemove() {
$pf = $this->getMock('PostFixtures', array('remove_all_posts', 'remove_all_categories'));
$pf->expects($this->once())->method('remove_all_posts');
$pf->expects($this->once())->method('remove_all_categories');
$pf->remove();
}
2009-11-23 13:16:55 +00:00
function providerTestProcessBlogOptions() {
return array(
array(
array(),
array('test' => 'test')
),
array(
array('test2' => 'test2'),
array('test' => 'test', 'test2' => 'test2')
),
array(
array('test' => false),
array('test' => false)
),
array(
array('test' => '${category:category}'),
array('test' => '1')
),
2009-11-23 13:24:00 +00:00
array(
array('test' => '${cat:category}'),
array('test' => '1')
),
2009-11-23 13:16:55 +00:00
);
}
/**
* @dataProvider providerTestProcessBlogOptions
*/
function testProcessBlogOptions($data, $expected_fields) {
update_option('test', 'test');
$this->pf->process_blog_options($data, array('category' => 1));
foreach ($expected_fields as $name => $value) {
$this->assertEquals($value, get_option($name));
2010-01-14 20:42:45 +00:00
if ($value === false) {
$all_options = get_alloptions();
$this->assertTrue(!isset($all_options[$name]));
}
2009-11-23 13:16:55 +00:00
}
}
2010-01-14 21:19:58 +00:00
2010-01-18 03:59:05 +00:00
function providerTestHandleJSON() {
2010-01-14 21:19:58 +00:00
return array(
2010-01-18 03:59:05 +00:00
array('', false),
array('{]', false),
array('{"test": "test"}', true),
2010-01-14 21:19:58 +00:00
);
}
/**
2010-01-18 03:59:05 +00:00
* @dataProvider providerTestHandleJSON
2010-01-14 21:19:58 +00:00
*/
2010-01-18 03:59:05 +00:00
function testHandleJSON($info, $should_succeed) {
2010-01-14 21:19:58 +00:00
$pf = $this->getMock('PostFixtures', array('process_data', 'remove', 'create'));
foreach (array('process_data', 'remove', 'create') as $method) {
$pf->expects($this->{$should_succeed ? 'once' : 'never'}())->method($method);
}
2010-01-18 03:59:05 +00:00
$pf->handle_json($info);
}
function providerTestHandleUpdate() {
return array(
array(
array(), false
),
array(
array('is_ok' => true, 'data' => 'json'), array('handle_json', 'json')
),
array(
array('is_ok' => true, 'data' => 'file:test'), array('handle_load_file', 'test')
),
);
}
/**
* @dataProvider providerTestHandleUpdate
*/
function testHandleUpdate($input, $expected_method) {
$pf = $this->getMock('PostFixtures', array('handle_load_file', 'handle_json'));
if (!empty($expected_method)) {
$pf->expects($this->once())->method($expected_method[0])->with($expected_method[1]);
}
$pf->handle_update($input);
2010-01-14 21:19:58 +00:00
}
function providerTestAdminInit() {
return array(
array(array(), false),
array(array('pf' => false), false),
array(array('pf' => array()), false),
array(array('pf' => array('_nonce' => false)), false),
array(array('pf' => array('_nonce' => 'post-fixtures')), true),
);
}
/**
* @dataProvider providerTestAdminInit
*/
function testAdminInit($post, $should_update) {
$_POST = $post;
$pf = $this->getMock('PostFixtures', array('handle_update'));
$pf->expects($this->{$should_update ? 'once' : 'never'}())->method('handle_update');
_set_valid_nonce('post-fixtures', 'post-fixtures');
$pf->admin_init();
$this->assertTrue(!isset($_POST['pf']));
}
2010-01-18 02:28:26 +00:00
function testRecurseDirectories() {
mkdir(vfsStream::url('root/dir/fixtures'), 0666, true);
foreach (array(
'dir/test.json',
'dir/test.inc',
'dir/fixtures/test',
'dir/fixtures/test.json',
'dir/fixtures/test.inc',
) as $path) {
$path = vfsStream::url("root/${path}");
file_put_contents($path, $path);
}
$pf = $this->getMock('PostFixtures', array('wpcontent_path'));
2010-01-18 02:34:24 +00:00
$pf->expects($this->any())->method('wpcontent_path')->will($this->returnValue(vfsStream::url('root')));
2010-01-18 02:28:26 +00:00
$this->assertEquals(array(
2010-01-18 02:34:24 +00:00
'dir/fixtures/test.json',
'dir/fixtures/test.inc',
2010-01-18 02:28:26 +00:00
), $pf->find_fixtures());
}
2009-11-22 20:50:16 +00:00
}