diff --git a/classes/WhatDidTheySay.php b/classes/WhatDidTheySay.php index ac904a0..c12695b 100644 --- a/classes/WhatDidTheySay.php +++ b/classes/WhatDidTheySay.php @@ -137,7 +137,7 @@ class WhatDidTheySay { $query = $wpdb->prepare("SELECT * FROM %s WHERE id = %d", $this->table, $update_info['id']); $result = $wpdb->get_results($query); - if (!empty($result)) { + if (is_array($result)) { if (count($result) == 1) { $result = $result[0]; foreach (array('language', 'transcript') as $field) { @@ -174,6 +174,28 @@ class WhatDidTheySay { } return false; } + + function add_transcription_to_post($transcription_id) { + global $wpdb; + + if ($this->is_user_allowed_to_update()) { + $query = $wpdb->prepare("SELECT * from %s WHERE id = %d", $this->table, $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); + + $query = $wpdb->prepare("DELETE FROM %s WHERE id = %d", $this->table, $transcription_id); + $result = $wpdb->query($query); + } + } + } + } + } } ?> diff --git a/test/WhatDidTheySayTest.php b/test/WhatDidTheySayTest.php index a60b192..87f2a25 100644 --- a/test/WhatDidTheySayTest.php +++ b/test/WhatDidTheySayTest.php @@ -256,6 +256,45 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase { $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')); + $what->expects($this->once()) + ->method('is_user_allowed_to_update') + ->will($this->returnValue(true)); + + 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); + } } ?>