diff --git a/classes/WhatDidTheySay.php b/classes/WhatDidTheySay.php index bbc0d06..ac904a0 100644 --- a/classes/WhatDidTheySay.php +++ b/classes/WhatDidTheySay.php @@ -20,14 +20,16 @@ class WhatDidTheySay { * @return bool True if the transcript was saved, false otherwise. */ function save_transcript($post_id, $language, $transcript) { - $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); + if ($this->is_user_allowed_to_update()) { + $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; } - return false; } /** @@ -51,19 +53,21 @@ class WhatDidTheySay { function get_queued_transcriptions_for_post($post_id) { global $wpdb; - $post = get_post($post_id); - if (!empty($post)) { - $query = $wpdb->prepare('SELECT * FROM %s WHERE post_id = %d', $this->table, $post_id); - $results = $wpdb->get_results($query); - if (!empty($results)) { - $valid_results = array(); - foreach ($results as $result) { - $user = get_userdata($result->user_id); - if (!empty($user)) { - $valid_results[] = $result; + if ($this->is_user_allowed_to_update()) { + $post = get_post($post_id); + if (!empty($post)) { + $query = $wpdb->prepare('SELECT * FROM %s WHERE post_id = %d', $this->table, $post_id); + $results = $wpdb->get_results($query); + 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 $valid_results; } } return false; @@ -77,24 +81,26 @@ class WhatDidTheySay { function add_queued_transcription_to_post($post_id, $transcript_info) { global $wpdb; - $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 = get_userdata($user_id); - if (!empty($user)) { - $query = $wpdb->prepare( - "INSERT INTO %s (post_id, user_id, language, transcript) VALUES (%d, %d, %s, %s)", - $this->table, $post_id, $user_id, $language, $transcript - ); - - return $wpdb->query($query); + if ($this->is_user_allowed_to_update()) { + $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 = get_userdata($user_id); + if (!empty($user)) { + $query = $wpdb->prepare( + "INSERT INTO %s (post_id, user_id, language, transcript) VALUES (%d, %d, %s, %s)", + $this->table, $post_id, $user_id, $language, $transcript + ); + + return $wpdb->query($query); + } } } } diff --git a/test/WhatDidTheySayTest.php b/test/WhatDidTheySayTest.php index 303db78..a60b192 100644 --- a/test/WhatDidTheySayTest.php +++ b/test/WhatDidTheySayTest.php @@ -15,13 +15,18 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase { function testSaveTranscription() { wp_insert_post(array('ID' => 1)); - $this->what->save_transcript(1, "en", "This is a transcript"); + $what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update')); + $what->expects($this->any()) + ->method('is_user_allowed_to_update') + ->will($this->returnValue(true)); + + $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"); + $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"); + $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)); } @@ -37,10 +42,15 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase { wp_insert_user(array('ID' => 1, 'first_name' => 'Test', 'last_name' => 'User')); wp_insert_post(array('ID' => 1)); - + + $what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update')); + $what->expects($this->any()) + ->method('is_user_allowed_to_update') + ->will($this->returnValue(true)); + $wpdb = $this->getMock('wpdb', array('get_results', 'prepare')); - $expected_query = sprintf("SELECT * FROM '%s' WHERE post_id = '%d'", $this->what->table, 1); + $expected_query = sprintf("SELECT * FROM '%s' WHERE post_id = '%d'", $what->table, 1); $wpdb->expects($this->once()) ->method('prepare') @@ -68,10 +78,10 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase { 'transcript' => 'This is a transcript' ) ), - $this->what->get_queued_transcriptions_for_post(1) + $what->get_queued_transcriptions_for_post(1) ); - $this->assertFalse($this->what->get_queued_transcriptions_for_post(2)); + $this->assertFalse($what->get_queued_transcriptions_for_post(2)); } function providerTestAddQueuedTranscriptionToPost() { @@ -113,6 +123,11 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase { ->method('prepare') ->will($this->returnValue($expected_query)); + $what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update')); + $what->expects($this->any()) + ->method('is_user_allowed_to_update') + ->will($this->returnValue(true)); + if ($expected_result === true) { $wpdb->expects($this->once()) ->method('query') @@ -120,7 +135,7 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase { ->will($this->returnValue(true)); } - $this->assertEquals($expected_result, $this->what->add_queued_transcription_to_post( + $this->assertEquals($expected_result, $what->add_queued_transcription_to_post( 1, array( 'user_id' => 1,