update authentication stuff

This commit is contained in:
John Bintz 2009-08-11 18:44:47 -04:00
parent 7bed32fbff
commit d1b1cb4834
2 changed files with 65 additions and 44 deletions

View File

@ -20,14 +20,16 @@ class WhatDidTheySay {
* @return bool True if the transcript was saved, false otherwise. * @return bool True if the transcript was saved, false otherwise.
*/ */
function save_transcript($post_id, $language, $transcript) { function save_transcript($post_id, $language, $transcript) {
$post = get_post($post_id); if ($this->is_user_allowed_to_update()) {
if (!empty($post)) { $post = get_post($post_id);
$current_transcripts = get_post_meta($post_id, "provided_transcripts", true); if (!empty($post)) {
if (!is_array($current_transcripts)) { $current_transcripts = array(); } $current_transcripts = get_post_meta($post_id, "provided_transcripts", true);
$current_transcripts[$language] = $transcript; if (!is_array($current_transcripts)) { $current_transcripts = array(); }
return update_post_meta($post_id, "provided_transcripts", $current_transcripts); $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) { function get_queued_transcriptions_for_post($post_id) {
global $wpdb; global $wpdb;
$post = get_post($post_id); if ($this->is_user_allowed_to_update()) {
if (!empty($post)) { $post = get_post($post_id);
$query = $wpdb->prepare('SELECT * FROM %s WHERE post_id = %d', $this->table, $post_id); if (!empty($post)) {
$results = $wpdb->get_results($query); $query = $wpdb->prepare('SELECT * FROM %s WHERE post_id = %d', $this->table, $post_id);
if (!empty($results)) { $results = $wpdb->get_results($query);
$valid_results = array(); if (!empty($results)) {
foreach ($results as $result) { $valid_results = array();
$user = get_userdata($result->user_id); foreach ($results as $result) {
if (!empty($user)) { $user = get_userdata($result->user_id);
$valid_results[] = $result; if (!empty($user)) {
$valid_results[] = $result;
}
} }
return $valid_results;
} }
return $valid_results;
} }
} }
return false; return false;
@ -77,24 +81,26 @@ class WhatDidTheySay {
function add_queued_transcription_to_post($post_id, $transcript_info) { function add_queued_transcription_to_post($post_id, $transcript_info) {
global $wpdb; global $wpdb;
$post = get_post($post_id); if ($this->is_user_allowed_to_update()) {
if (!empty($post)) { $post = get_post($post_id);
$transcript_info = (array)$transcript_info; if (!empty($post)) {
if (!empty($transcript_info)) { $transcript_info = (array)$transcript_info;
$ok = true; if (!empty($transcript_info)) {
foreach (array('language', 'transcript') as $field) { $ok = true;
if (empty($transcript_info[$field])) { $ok = false; break; } foreach (array('language', 'transcript') as $field) {
} if (empty($transcript_info[$field])) { $ok = false; break; }
if ($ok) { }
extract($transcript_info); if ($ok) {
$user = get_userdata($user_id); extract($transcript_info);
if (!empty($user)) { $user = get_userdata($user_id);
$query = $wpdb->prepare( if (!empty($user)) {
"INSERT INTO %s (post_id, user_id, language, transcript) VALUES (%d, %d, %s, %s)", $query = $wpdb->prepare(
$this->table, $post_id, $user_id, $language, $transcript "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);
return $wpdb->query($query);
}
} }
} }
} }

View File

@ -15,13 +15,18 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase {
function testSaveTranscription() { function testSaveTranscription() {
wp_insert_post(array('ID' => 1)); 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->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->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)); $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_user(array('ID' => 1, 'first_name' => 'Test', 'last_name' => 'User'));
wp_insert_post(array('ID' => 1)); 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')); $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()) $wpdb->expects($this->once())
->method('prepare') ->method('prepare')
@ -68,10 +78,10 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase {
'transcript' => 'This is a transcript' '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() { function providerTestAddQueuedTranscriptionToPost() {
@ -113,6 +123,11 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase {
->method('prepare') ->method('prepare')
->will($this->returnValue($expected_query)); ->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) { if ($expected_result === true) {
$wpdb->expects($this->once()) $wpdb->expects($this->once())
->method('query') ->method('query')
@ -120,7 +135,7 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase {
->will($this->returnValue(true)); ->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, 1,
array( array(
'user_id' => 1, 'user_id' => 1,