break out authentication

This commit is contained in:
John Bintz 2009-08-11 17:14:46 -04:00
parent 1e6b3b64fb
commit 92c07d5a1e
2 changed files with 66 additions and 55 deletions

View File

@ -102,13 +102,7 @@ class WhatDidTheySay {
return false; return false;
} }
/** function is_user_allowed_to_update() {
* Update a queued transcript.
* @param array $update_info The info on the transcript being updated.
*/
function update_queued_transcript($update_info) {
global $wpdb;
$options = get_option('what-did-they-say-options'); $options = get_option('what-did-they-say-options');
$user_info = wp_get_current_user(); $user_info = wp_get_current_user();
@ -121,8 +115,18 @@ class WhatDidTheySay {
$ok = in_array($user_info->ID, $options['allowed_users']); $ok = in_array($user_info->ID, $options['allowed_users']);
} }
} }
return $ok;
}
if ($ok) { /**
* Update a queued transcript.
* @param array $update_info The info on the transcript being updated.
*/
function update_queued_transcript($update_info) {
global $wpdb;
if ($this->is_user_allowed_to_update()) {
$query = $wpdb->prepare("SELECT * FROM %s WHERE id = %d", $this->table, $update_info['id']); $query = $wpdb->prepare("SELECT * FROM %s WHERE id = %d", $this->table, $update_info['id']);
$result = $wpdb->get_results($query); $result = $wpdb->get_results($query);

View File

@ -13,6 +13,8 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase {
} }
function testSaveTranscription() { function testSaveTranscription() {
wp_insert_post(array('ID' => 1));
$this->what->save_transcript(1, "en", "This is a transcript"); $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->assertEquals(array("en" => "This is a transcript"), get_post_meta(1, "provided_transcripts", true));
@ -128,33 +130,49 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase {
)); ));
} }
function providerTestIsUserAllowedToUpdate() {
return array(
array(
false, array(), array(), 1, false
),
array(
false, array('edit_posts'), array(), 1, true
),
array(
true, array(), array(2), 1, false
),
array(
true, array(), array(1), 1, true
),
);
}
/**
* @dataProvider providerTestIsUserAllowedToUpdate
*/
function testIsUserAllowedToUpdate($only_allowed_users, $current_user_can, $allowed_users, $current_user_id, $expected_result) {
update_option('what-did-they-say-options', array('allowed_users' => $allowed_users, 'only_allowed_users' => $only_allowed_users));
_set_user_capabilities($current_user_can);
wp_insert_user(array('ID' => 1, 'first_name' => 'Test', 'last_name' => 'User'));
wp_set_current_user($current_user_id);
$this->assertEquals($expected_result, $this->what->is_user_allowed_to_update());
}
function providerTestUpdateQueuedTranscription() { function providerTestUpdateQueuedTranscription() {
return array( return array(
array( array(
false, array(), array(), array(), 1, array("language" => "en", "transcript" => "This") array(), array("language" => "en", "transcript" => "This")
), ),
array( array(
false, array('edit_posts'), array(), array(), 1, array("language" => "en", "transcript" => "This") array(
(object)array('ID' => 1)
), array("language" => "en", "transcript" => "This")
), ),
array( array(
false, array('edit_posts'), array( array(
(object)array('ID' => 1) (object)array('ID' => 1)
), array(), 1, array("language" => "en", "transcript" => "This") ), array("language" => "en", "transcript" => "This", 'id' => 1)
),
array(
false, array('edit_posts'), array(
(object)array('ID' => 1)
), array(), 1, array("language" => "en", "transcript" => "This", 'id' => 1)
),
array(
true, array(), array(
(object)array('ID' => 1)
), array(2), 1, array("language" => "en", "transcript" => "This", 'id' => 1)
),
array(
true, array(), array(
(object)array('ID' => 1)
), array(1), 1, array("language" => "en", "transcript" => "This", 'id' => 1)
), ),
); );
} }
@ -162,44 +180,33 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase {
/** /**
* @dataProvider providerTestUpdateQueuedTranscription * @dataProvider providerTestUpdateQueuedTranscription
*/ */
function testUpdateQueuedTranscription($only_allowed_users, $current_user_can, $valid_transcripts, $allowed_users, $current_user_id, $update_info) { function testUpdateQueuedTranscription($valid_transcripts, $update_info) {
global $wpdb; global $wpdb;
$what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update'));
$what->expects($this->once())
->method('is_user_allowed_to_update')
->will($this->returnValue(true));
$wpdb = $this->getMock('wpdb', array('prepare', 'get_results', 'query')); $wpdb = $this->getMock('wpdb', array('prepare', 'get_results', 'query'));
update_option('what-did-they-say-options', array('allowed_users' => $allowed_users, 'only_allowed_users' => $only_allowed_users));
_set_user_capabilities($current_user_can); $wpdb->expects($this->once())
->method('get_results')
if ($only_allowed_users) { ->will($this->returnValue($valid_transcripts));
$will_search_transcripts = (in_array($current_user_id, $allowed_users));
} else { $in_array = false;
$will_search_transcripts = true; foreach ($valid_transcripts as $transcript) {
if (!in_array('edit_posts', $current_user_can)) { if ($transcript->id == $update_info['id']) { $in_array = true; break; }
$will_search_transcripts = (in_array($current_user_id, $allowed_users));
}
} }
if ($will_search_transcripts) { if ($in_array) {
$wpdb->expects($this->once()) $wpdb->expects($this->once())
->method('get_results') ->method('query');
->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)); wp_insert_post(array('ID' => 1));
wp_insert_user(array('ID' => 1, 'first_name' => 'Test', 'last_name' => 'User'));
wp_set_current_user($current_user_id);
$this->what->update_queued_transcript($update_info); $what->update_queued_transcript($update_info);
} }
} }