From 8e5f1ce4bd02d85f08100ae35adf94e7f7dec3e2 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 23 Oct 2009 07:04:53 -0400 Subject: [PATCH] add import processing --- classes/WhatDidTheySayAdmin.inc | 24 +++++++++++++++ test/WhatDidTheySayAdminTest.php | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/classes/WhatDidTheySayAdmin.inc b/classes/WhatDidTheySayAdmin.inc index 1e94553..8419167 100644 --- a/classes/WhatDidTheySayAdmin.inc +++ b/classes/WhatDidTheySayAdmin.inc @@ -38,6 +38,8 @@ class WhatDidTheySayAdmin { var $is_ajax = false; + var $_import_chunk_size = 50; + /** * Initialize the admin interface. * @param WhatDidTheySay $what_did_they_say The WhatDidTheySay object to use for all transcript transactions. @@ -549,6 +551,28 @@ class WhatDidTheySayAdmin { return $ok; } + function import_transcripts($language_code) { + $modified = false; + $posts = get_posts(array('posts_per_page' => $this->_import_chunk_size, 'meta_key' => 'transcript')); + if (!empty($posts)) { + $modified = 0; + foreach ($posts as $post) { + if ($transcript = get_post_meta($post->ID, 'transcript', true)) { + $approved_transcript_manager = new WDTSApprovedTranscript($post->ID); + $approved_transcript_manager->save_transcript(array( + 'language' => $language_code, + 'transcript' => $transcript + )); + + delete_post_meta($post->ID, 'transcript'); + + $modified++; + } + } + if ($modified == 0) { $modified = false; } + } + return $modified; + } /** Handle data updates **/ diff --git a/test/WhatDidTheySayAdminTest.php b/test/WhatDidTheySayAdminTest.php index b39fdf1..d08a4f6 100644 --- a/test/WhatDidTheySayAdminTest.php +++ b/test/WhatDidTheySayAdminTest.php @@ -2,7 +2,9 @@ require_once('PHPUnit/Framework.php'); require_once('MockPress/mockpress.php'); + require_once(dirname(__FILE__) . '/../classes/WhatDidTheySayAdmin.inc'); +require_once(dirname(__FILE__) . '/../classes/WDTSTranscriptClasses.inc'); class WhatDidTheySayAdminTest extends PHPUnit_Framework_TestCase { function setUp() { @@ -78,6 +80,54 @@ class WhatDidTheySayAdminTest extends PHPUnit_Framework_TestCase { 'approve_transcriptions' => true ), get_usermeta(1, 'transcript_capabilities')); } + + /** + * Integration. + */ + function testPerformImport() { + $admin = new WhatDidTheySayAdmin(); + $admin->_import_chunk_size = 1; + + wp_insert_user(array('ID' => 1)); + wp_set_current_user(1); + + _set_user_capabilities('approve_transcriptions'); + + for ($i = 1; $i <= 2; ++$i) { + wp_insert_post(array('ID' => $i)); + update_post_meta($i, "transcript", "this is my transcript"); + } + + _set_up_get_posts_response(array( + 'posts_per_page' => 1, + 'meta_key' => 'transcript' + ), array( + get_post(1) + )); + + $this->assertEquals(1, $admin->import_transcripts('en')); + + $this->assertEquals('', get_post_meta(1, "transcript", true)); + $this->assertEquals('this is my transcript', get_post_meta(2, "transcript", true)); + + $this->assertEquals(array( + array( + 'language' => 'en', + 'transcript' => 'this is my transcript', + 'user_id' => 1, + 'key' => 0 + ) + ), get_post_meta(1, 'approved_transcripts', true)); + + delete_post_meta(2, 'transcript'); + + _set_up_get_posts_response(array( + 'posts_per_page' => 1, + 'meta_key' => 'transcript' + ), array()); + + $this->assertEquals(false, $admin->import_transcripts('en')); + } } ?> \ No newline at end of file