From 712708a9967ac49d463bd456eb52d0d737918f23 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 5 Sep 2009 16:05:15 -0400 Subject: [PATCH] move transcript handling to separate class --- classes/WDTSTranscript.php | 92 ++++++++++++++ classes/WhatDidTheySay.php | 233 +---------------------------------- test/WDTSTranscriptTest.php | 117 ++++++++++++++++++ test/WhatDidTheySayTest.php | 236 ------------------------------------ 4 files changed, 210 insertions(+), 468 deletions(-) create mode 100644 classes/WDTSTranscript.php create mode 100644 test/WDTSTranscriptTest.php delete mode 100644 test/WhatDidTheySayTest.php diff --git a/classes/WDTSTranscript.php b/classes/WDTSTranscript.php new file mode 100644 index 0000000..74168a3 --- /dev/null +++ b/classes/WDTSTranscript.php @@ -0,0 +1,92 @@ +post_id = $post_id; } + } + + function _get_transcripts_metadata() { + $transcripts = false; + if (!is_null($this->key)) { + $post = get_post($this->post_id); + if (!empty($post)) { + $transcripts = get_post_meta($this->post_id, $this->key, true); + if (!is_array($transcripts)) { $transcripts = array(); } + } + } + return $transcripts; + } + + /** + * Save a transcript to a post. + * @param int $post_id The post to attach the transcript to. + * @param string $language The language of the transcript. + * @param string $transcript The transcript content. + * @return bool True if the transcript was saved, false otherwise. + */ + function save_transcript($transcript_info) { + $user = wp_get_current_user(); + if (!empty($user)) { + $transcript_info = (array)$transcript_info; + $transcript_info['user_id'] = $user->ID; + + if (($transcripts = $this->_get_transcripts_metadata()) !== false) { + $new_transcripts = array(); + $was_added = false; + foreach ($transcripts as $transcript) { + if ($transcript['language'] == $transcript_info['language']) { + $was_added = true; + $new_transcripts[] = $transcript_info; + } else { + $new_transcripts[] = $transcript; + } + } + if (!$was_added) { $new_transcripts[] = $transcript_info; } + + return update_post_meta($this->post_id, $this->key, $new_transcripts); + } + } + return false; + } + + function delete_transcript($language = null) { + if (($transcripts = $this->_get_transcripts_metadata()) !== false) { + $new_transcripts = array(); + foreach ($transcripts as $transcript) { + if ($transcript['language'] != $language) { $new_transcripts[] = $transcript; } + } + + return update_post_meta($this->post_id, $this->key, $new_transcripts); + } + return false; + } + + function get_transcripts() { + return $this->_get_transcripts_metadata(); + } + + function get_transcripts_for_user($user_id) { + $user_transcripts = array(); + if (($transcripts = $this->_get_transcripts_metadata()) !== false) { + foreach ($transcripts as $transcript) { + if ($transcript['user_id'] == $user_id) { $user_transcripts[] = $transcript; } + } + } + return $user_transcripts; + } + + function get_languages() { + $languages = array(); + if (($transcripts = $this->_get_transcripts_metadata()) !== false) { + foreach ($transcripts as $transcript) { + $languages[$transcript['language']] = true; + } + } + return array_keys($languages); + } +} + +?> \ No newline at end of file diff --git a/classes/WhatDidTheySay.php b/classes/WhatDidTheySay.php index a7a66ba..e7c7c18 100644 --- a/classes/WhatDidTheySay.php +++ b/classes/WhatDidTheySay.php @@ -6,241 +6,10 @@ * other posts as necessary. */ class WhatDidTheySay { - var $version = "0.1"; - /** * Constructor. */ - function WhatDidTheySay() { - global $wpdb; - - $this->table = $wpdb->prefix . "provided_transcripts"; - } - - /** - * Handle plugin activation. - */ - function install() { - if (get_option('what-did-they-say-version') !== $this->version) { - $sql = "CREATE TABLE %s ( - id int NOT NULL AUTO_INCREMENT, - post_id int NOT NULL, - user_id int NOT NULL, - language char(10) NOT NULL, - transcript mediumtext, - UNIQUE KEY id (id) - );"; - - require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); - dbDelta(sprintf($sql, $this->table)); - - update_option('what-did-they-say-version', $this->version); - } - } - - /** - * Save a transcript to a post. - * @param int $post_id The post to attach the transcript to. - * @param string $language The language of the transcript. - * @param string $transcript The transcript content. - * @return bool True if the transcript was saved, false otherwise. - */ - function save_transcript($post_id, $language, $transcript) { - if (current_user_can('submit_transcriptions')) { - $post = get_post($post_id); - if (!empty($post)) { - $current_transcripts = get_post_meta($post_id, "provided_transcripts", true); - if (!is_array($current_transcripts)) { $current_transcripts = array(); } - $current_transcripts[$language] = $transcript; - return update_post_meta($post_id, "provided_transcripts", $current_transcripts); - } - return false; - } - } - - /** - * Get all transcripts for the indicated post. - * @param int $post_id The post ID to search. - * @return array|false The provided transcripts for the post, or false if no transcripts found. - */ - function get_transcripts($post_id) { - return get_post_meta($post_id, 'provided_transcripts', true); - } - - /** - * Get the languages that the approved transcripts for the post are written in. - * @param int $post_id The post ID to check for transcripts. - * @return array|false The languages for the transcripts, or false if none found. - */ - function get_transcript_languages($post_id) { - $current_transcripts = get_post_meta($post_id, "provided_transcripts", true); - if (is_array($current_transcripts)) { - return array_keys($current_transcripts); - } - return false; - } - - /** - * Get the queued transcriptions for the provided post. - * @param int $post_id The post to search for transcripts. - * @return array|false The array of transcripts for the post, or false if the post is invalid. - */ - function get_queued_transcriptions_for_post($post_id) { - if (current_user_can('submit_transcriptions')) { - $post = get_post($post_id); - if (!empty($post)) { - $results = get_post_meta($post_id, "queued_transcripts", true); - if (!empty($results)) { - $valid_results = array(); - foreach ($results as $result) { - $user = get_userdata($result['user_id']); - if (!empty($user)) { - $valid_results[] = $result; - } - } - return $valid_results; - } - } - } - return false; - } - - /** - * Queue a transcription to a post. - * @param int $post_id The post to attach the transcription to. - * @param array $transcript_info The new transcript's info. - */ - function add_queued_transcription_to_post($post_id, $transcript_info) { - if (current_user_can('submit_transcriptions')) { - $post = get_post($post_id); - if (!empty($post)) { - $transcript_info = (array)$transcript_info; - if (!empty($transcript_info)) { - $ok = true; - foreach (array('language', 'transcript') as $field) { - if (empty($transcript_info[$field])) { $ok = false; break; } - } - if ($ok) { - extract($transcript_info); - $user = wp_get_current_user(); - if (!empty($user)) { - $current_transcriptions = get_post_meta($post_id, 'queued_transcripts', true); - $transcript_info['user_id'] = $user->ID; - $current_transcriptions[] = $transcript_info; - update_post_meta($post_id, 'queued_transcripts', $current_transcriptions); - - return true; - } - } - } - } - } - return false; - } - - /** - * Update a queued transcript. - * @param array $update_info The info on the transcript being updated. - * @return boolean True if the transcript was updated. - */ - function update_queued_transcription($update_info) { - global $wpdb; - - if (current_user_can('submit_transcriptions')) { - $query = $wpdb->prepare("SELECT * FROM " . $this->table . " WHERE id = %d", $update_info['id']); - $result = $wpdb->get_results($query); - - if (is_array($result)) { - if (count($result) == 1) { - $result = $result[0]; - foreach (array('language', 'transcript') as $field) { - $result->{$field} = $update_info[$field]; - } - $query = $wpdb->prepare( - "UPDATE " . $this->table . " SET language = %s, transcript = %s WHERE id = %d", - $result->language, $result->transcript, $result->id - ); - $wpdb->query($query); - return true; - } - } - } - return false; - } - - /** - * Delete a queued transcript. - * @param int $transcription_id The transcription to delete. - * @return boolean True if the transcript was deleted. - */ - function delete_queued_transcription($transcription_id) { - global $wpdb; - - if (current_user_can('submit_transcriptions')) { - $query = $wpdb->prepare("DELETE FROM " . $this->table . " WHERE id = %d", $transcription_id); - return $wpdb->query($query); - } - return false; - } - - /** - * Add a queued transcription to its associated post. - * @param int $transcription_id The transcription ID to approve. - */ - function add_transcription_to_post($transcription_id) { - global $wpdb; - - if (current_user_can('approve_transcriptions')) { - $query = $wpdb->prepare("SELECT * from " . $this->table . " WHERE id = %d", $transcription_id); - $result = $wpdb->get_results($query); - if (is_array($result)) { - if (count($result) == 1) { - $result = (object)$result[0]; - - $post = get_post($result->post_id); - if (!empty($post)) { - $this->save_transcript($result->post_id, $result->language, $result->transcript); - $this->delete_queued_transcription($transcription_id); - } - } - } - } - } - - /** - * Get all the queued transcriptions for a particular user and post. - * @param int $user_id The user ID to search for. - * @param int $post_id The post ID to search for. - * @return array|false The queued transcriptions for this user, or false if they don't have permissions to see queued transcriptions. - */ - function get_queued_transcriptions_for_user_and_post($user_id, $post_id) { - global $wpdb; - - if (current_user_can('submit_transcriptions')) { - $query = $wpdb->prepare("SELECT * FROM " . $this->table . " WHERE user_id = %d AND post_id = %d", $user_id, $post_id); - return $wpdb->get_results($query); - } - return false; - } - - /** - * Delete a transcript for a particular language from a post. - * @param int $post_id The post to search. - * @param string $language The language code to search for. - * @return boolean True if the transcript was deleted. - */ - function delete_transcript($post_id, $language) { - if (current_user_can('approve_transcriptions')) { - $post = get_post($post_id); - if (!empty($post)) { - $current_transcripts = get_post_meta($post_id, "provided_transcripts", true); - unset($current_transcripts[$language]); - update_post_meta($post_id, "provided_transcripts", $current_transcripts); - return true; - } - } - return false; - } + function WhatDidTheySay() {} /** * Get the default transcript language for this blog. diff --git a/test/WDTSTranscriptTest.php b/test/WDTSTranscriptTest.php new file mode 100644 index 0000000..22c9dfb --- /dev/null +++ b/test/WDTSTranscriptTest.php @@ -0,0 +1,117 @@ + 1)); + wp_set_current_user(1); + wp_insert_post(array('ID' => 1)); + + $this->w = new WDTSTranscriptManager(1); + $this->w->key = "test"; + } + + function testSaveTranscript() { + $this->w->save_transcript(array( + 'language' => 'en', + 'transcript' => 'this is a transcript' + )); + + $this->assertEquals( + array( + array( + 'language' => 'en', + 'transcript' => 'this is a transcript', + 'user_id' => 1 + ) + ), + get_post_meta(1, $this->w->key, true) + ); + + $this->w->save_transcript(array( + 'language' => 'en', + 'transcript' => 'this is another transcript' + )); + + $this->assertEquals( + array( + array( + 'language' => 'en', + 'transcript' => 'this is another transcript', + 'user_id' => 1 + ) + ), + get_post_meta(1, $this->w->key, true) + ); + + $this->w->save_transcript(array( + 'language' => 'fr', + 'transcript' => "il s'agit d'une nouvelle transcription" + )); + + $this->assertEquals( + array( + array( + 'language' => 'en', + 'transcript' => 'this is another transcript', + 'user_id' => 1 + ), + array( + 'language' => 'fr', + 'transcript' => "il s'agit d'une nouvelle transcription", + 'user_id' => 1 + ), + ), + get_post_meta(1, $this->w->key, true) + ); + } + + function testDeleteTranscript() { + update_post_meta(1, $this->w->key, array( + array( + 'language' => 'en', + 'transcript' => 'this is another transcript', + 'user_id' => 1 + ), + array( + 'language' => 'fr', + 'transcript' => "il s'agit d'une nouvelle transcription", + 'user_id' => 1 + ), + )); + + $this->w->delete_transcript('en'); + + $this->assertEquals(array( + array( + 'language' => 'fr', + 'transcript' => "il s'agit d'une nouvelle transcription", + 'user_id' => 1 + ), + ), get_post_meta(1, $this->w->key, true)); + } + + function testGetLanguages() { + update_post_meta(1, $this->w->key, array( + array( + 'language' => 'en', + 'transcript' => 'this is another transcript', + 'user_id' => 1 + ), + array( + 'language' => 'fr', + 'transcript' => "il s'agit d'une nouvelle transcription", + 'user_id' => 1 + ), + )); + + $this->assertEquals(array('en', 'fr'), $this->w->get_languages()); + } +} + +?> \ No newline at end of file diff --git a/test/WhatDidTheySayTest.php b/test/WhatDidTheySayTest.php deleted file mode 100644 index 240366c..0000000 --- a/test/WhatDidTheySayTest.php +++ /dev/null @@ -1,236 +0,0 @@ -what = new WhatDidTheySay(); - } - - function testSaveTranscription() { - wp_insert_post(array('ID' => 1)); - - $this->what->save_transcript(1, "en", "This is a transcript"); - $this->assertEquals(array("en" => "This is a transcript"), get_post_meta(1, "provided_transcripts", true)); - - $this->what->save_transcript(1, "en", "this is a new transcript"); - $this->assertEquals(array("en" => "this is a new transcript"), get_post_meta(1, "provided_transcripts", true)); - - $this->what->save_transcript(1, "fr", "il s'agit d'une nouvelle transcription"); - $this->assertEquals(array("en" => "this is a new transcript", "fr" => "il s'agit d'une nouvelle transcription"), get_post_meta(1, "provided_transcripts", true)); - } - - function testGetListOfLanguagesForPost() { - update_post_meta(1, "provided_transcripts", array('en' => 'this is a new transcript', 'fr' => "il s'agit d'une nouvelle transcription")); - $this->assertEquals(array('en', 'fr'), $this->what->get_transcript_languages(1)); - - $this->assertEquals(false, $this->what->get_transcript_languages(2)); - } - - function testGetQueuedTranscriptionsForPost() { - wp_insert_user(array('ID' => 1, 'first_name' => 'Test', 'last_name' => 'User')); - wp_insert_post(array('ID' => 1)); - - update_post_meta(1, "queued_transcripts", array( - array('user_id' => 1, 'language' => 'en', 'transcript' => 'This is a transcript') - )); - - $this->assertEquals( - array( - array( - 'user_id' => 1, - 'language' => 'en', - 'transcript' => 'This is a transcript' - ) - ), - $this->what->get_queued_transcriptions_for_post(1) - ); - - $this->assertFalse($what->get_queued_transcriptions_for_post(2)); - } - - function providerTestAddQueuedTranscriptionToPost() { - return array( - array( - array(1, 1, "en", "This is a transcript"), true - ), - array( - array(2, 1, "en", "This is a transcript"), false - ), - array( - array(1, 2, "en", "This is a transcript"), false - ), - array( - array(1, 1, "", "This is a transcript"), false - ), - array( - array(1, 1, "en", ""), false - ), - ); - } - - /** - * @dataProvider providerTestAddQueuedTranscriptionToPost - */ - function testAddQueuedTranscriptionToPost($query_settings, $expected_result) { - wp_insert_user(array('ID' => 1, 'first_name' => 'Test', 'last_name' => 'User')); - wp_insert_post(array('ID' => 1)); - - wp_set_current_user(1); - - $what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update')); - - $this->assertEquals($expected_result, $what->add_queued_transcription_to_post( - 1, - array( - 'language' => 'en', - 'transcript' => "This is a transcript" - ) - )); - - if ($expected_result) { - $this->assertEquals(array( - array('user_id' => 1, 'language' => 'en', 'transcript' => 'This is a transcript') - ), get_post_meta(1, "queued_transcripts", true)); - - $what->add_queued_transcription_to_post( - 1, array( - 'language' => 'fr', - 'transcript' => "il s'agit d'une transcription" - ) - ); - - $this->assertEquals(array( - array('user_id' => 1, 'language' => 'en', 'transcript' => 'This is a transcript'), - array('user_id' => 1, 'language' => 'fr', 'transcript' => "il s'agit d'une transcription") - ), get_post_meta(1, "queued_transcripts", true)); - } - } - - function providerTestUpdateQueuedTranscription() { - return array( - array( - array(), array("language" => "en", "transcript" => "This") - ), - array( - array( - (object)array('ID' => 1) - ), array("language" => "en", "transcript" => "This") - ), - array( - array( - (object)array('ID' => 1) - ), array("language" => "en", "transcript" => "This", 'id' => 1) - ), - ); - } - - /** - * @dataProvider providerTestUpdateQueuedTranscription - */ - function testUpdateQueuedTranscription($valid_transcripts, $update_info) { - global $wpdb; - - $what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update')); - - $wpdb = $this->getMock('wpdb', array('prepare', 'get_results', 'query')); - - $wpdb->expects($this->once()) - ->method('get_results') - ->will($this->returnValue($valid_transcripts)); - - $in_array = false; - foreach ($valid_transcripts as $transcript) { - if ($transcript->id == $update_info['id']) { $in_array = true; break; } - } - - if ($in_array) { - $wpdb->expects($this->once()) - ->method('query'); - } - - wp_insert_post(array('ID' => 1)); - - $what->update_queued_transcription($update_info); - } - - function providerTestDeleteQueuedTranscription() { - return array( - array(array(), 1, false), - array(array(2), 1, false), - array(array(1), 1, true) - ); - } - - /** - * @dataProvider providerTestDeleteQueuedTranscription - */ - function testDeleteQueuedTranscription($valid_transcripts, $transcript_id_to_delete, $expected_result) { - global $wpdb; - - $what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update')); - - $wpdb = $this->getMock('wpdb', array('prepare', 'get_var', 'query')); - - if (in_array($transcript_id_to_delete, $valid_transcripts)) { - $wpdb->expects($this->once()) - ->method('query'); - } - - $what->delete_queued_transcription($transcript_id_to_delete); - } - - function providerTestAddTranscriptionToPost() { - return array( - array(null, false), - array((object)array('id' => 1, 'post_id' => 2), false), - array((object)array('id' => 1, 'post_id' => 1, 'language' => 'en', 'transcript' => 'This is a transcript'), true) - ); - } - - /** - * @dataProvider providerTestAddTranscriptionToPost - */ - function testAddTranscriptionToPost($get_results_return, $expects_delete) { - global $wpdb; - - $what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update', 'save_transcript')); - - wp_insert_post((object)array('ID' => 1)); - - $wpdb = $this->getMock('wpdb', array('get_results', 'prepare', 'query')); - - $wpdb->expects($this->once()) - ->method('get_results') - ->will($this->returnValue(array($get_results_return))); - - if ($expects_delete) { - $wpdb->expects($this->at(2)) - ->method('prepare') - ->with(new PHPUnit_Framework_Constraint_PCREMatch('#DELETE FROM .* WHERE id = .*#')); - - $what->expects($this->once()) - ->method('save_transcript'); - } - - $what->add_transcription_to_post(1); - } - - function testDeleteTranscript() { - $what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update')); - - wp_insert_post((object)array('ID' => 1)); - update_post_meta(1, "provided_transcripts", array("en" => "This is a transcript")); - - $what->delete_transcript(1, "en"); - - $this->assertEquals(array(), get_post_meta(1, "provided_transcripts", true)); - } -} - -?>