From 92c07d5a1ec2fe5b0e6ca40f70238aec6bfbd833 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 11 Aug 2009 17:14:46 -0400 Subject: [PATCH] break out authentication --- classes/WhatDidTheySay.php | 20 ++++--- test/WhatDidTheySayTest.php | 101 +++++++++++++++++++----------------- 2 files changed, 66 insertions(+), 55 deletions(-) diff --git a/classes/WhatDidTheySay.php b/classes/WhatDidTheySay.php index 710d604..c7ccced 100644 --- a/classes/WhatDidTheySay.php +++ b/classes/WhatDidTheySay.php @@ -102,13 +102,7 @@ class WhatDidTheySay { return false; } - /** - * Update a queued transcript. - * @param array $update_info The info on the transcript being updated. - */ - function update_queued_transcript($update_info) { - global $wpdb; - + function is_user_allowed_to_update() { $options = get_option('what-did-they-say-options'); $user_info = wp_get_current_user(); @@ -121,8 +115,18 @@ class WhatDidTheySay { $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']); $result = $wpdb->get_results($query); diff --git a/test/WhatDidTheySayTest.php b/test/WhatDidTheySayTest.php index 5b196e6..199f5c5 100644 --- a/test/WhatDidTheySayTest.php +++ b/test/WhatDidTheySayTest.php @@ -13,6 +13,8 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase { } 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)); @@ -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() { return array( array( - false, array(), array(), array(), 1, array("language" => "en", "transcript" => "This") + array(), array("language" => "en", "transcript" => "This") ), array( - false, array('edit_posts'), array(), array(), 1, array("language" => "en", "transcript" => "This") + array( + (object)array('ID' => 1) + ), array("language" => "en", "transcript" => "This") ), array( - false, array('edit_posts'), array( + array( (object)array('ID' => 1) - ), array(), 1, array("language" => "en", "transcript" => "This") - ), - 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) + ), array("language" => "en", "transcript" => "This", 'id' => 1) ), ); } @@ -162,44 +180,33 @@ class WhatDidTheySayTest extends PHPUnit_Framework_TestCase { /** * @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; + $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')); - update_option('what-did-they-say-options', array('allowed_users' => $allowed_users, 'only_allowed_users' => $only_allowed_users)); - _set_user_capabilities($current_user_can); - - if ($only_allowed_users) { - $will_search_transcripts = (in_array($current_user_id, $allowed_users)); - } else { - $will_search_transcripts = true; - if (!in_array('edit_posts', $current_user_can)) { - $will_search_transcripts = (in_array($current_user_id, $allowed_users)); - } + $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 ($will_search_transcripts) { - $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'); - } + if ($in_array) { + $wpdb->expects($this->once()) + ->method('query'); } 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); } }