what-did-they-say/test/WhatDidTheySayTest.php

262 lines
8.3 KiB
PHP
Raw Normal View History

2009-08-09 23:53:44 +00:00
<?php
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
require_once(dirname(__FILE__) . '/../classes/WhatDidTheySay.php');
class WhatDidTheySayTest extends PHPUnit_Framework_TestCase {
function setUp() {
global $wpdb;
$this->what = new WhatDidTheySay();
_reset_wp();
$wpdb = null;
}
function testSaveTranscription() {
2009-08-11 21:14:46 +00:00
wp_insert_post(array('ID' => 1));
2009-08-11 22:44:47 +00:00
$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");
2009-08-09 23:53:44 +00:00
$this->assertEquals(array("en" => "This is a transcript"), get_post_meta(1, "provided_transcripts", true));
2009-08-11 22:44:47 +00:00
$what->save_transcript(1, "en", "this is a new transcript");
2009-08-09 23:53:44 +00:00
$this->assertEquals(array("en" => "this is a new transcript"), get_post_meta(1, "provided_transcripts", true));
2009-08-11 22:44:47 +00:00
$what->save_transcript(1, "fr", "il s'agit d'une nouvelle transcription");
2009-08-09 23:53:44 +00:00
$this->assertEquals(array("en" => "this is a new transcript", "fr" => "il s'agit d'une nouvelle transcription"), get_post_meta(1, "provided_transcripts", true));
}
function testGetListOfLanguagesForPost() {
update_post_meta(1, "provided_transcripts", array('en' => 'this is a new transcript', 'fr' => "il s'agit d'une nouvelle transcription"));
$this->assertEquals(array('en', 'fr'), $this->what->get_transcript_languages(1));
$this->assertEquals(false, $this->what->get_transcript_languages(2));
}
function testGetQueuedTranscriptionsForPost() {
global $wpdb;
wp_insert_user(array('ID' => 1, 'first_name' => 'Test', 'last_name' => 'User'));
wp_insert_post(array('ID' => 1));
2009-08-11 22:44:47 +00:00
$what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update'));
$what->expects($this->any())
->method('is_user_allowed_to_update')
->will($this->returnValue(true));
2009-08-10 16:50:30 +00:00
$wpdb = $this->getMock('wpdb', array('get_results', 'prepare'));
2009-08-11 22:44:47 +00:00
$expected_query = sprintf("SELECT * FROM '%s' WHERE post_id = '%d'", $what->table, 1);
2009-08-10 16:50:30 +00:00
$wpdb->expects($this->once())
->method('prepare')
->will($this->returnValue($expected_query));
2009-08-09 23:53:44 +00:00
$wpdb->expects($this->once())
->method('get_results')
2009-08-10 16:50:30 +00:00
->with($expected_query)
2009-08-09 23:53:44 +00:00
->will(
$this->returnValue(
array(
(object)array('id' => 1, 'post_id' => 1, 'user_id' => 1, 'language' => 'en', 'transcript' => "This is a transcript"),
(object)array('id' => 2, 'post_id' => 1, 'user_id' => 2, 'language' => 'fr', 'transcript' => "il s'agit d'une nouvelle transcription"),
)
)
);
$this->assertEquals(
array(
(object)array(
'id' => 1,
'post_id' => 1,
'user_id' => 1,
'language' => 'en',
'transcript' => 'This is a transcript'
)
),
2009-08-11 22:44:47 +00:00
$what->get_queued_transcriptions_for_post(1)
2009-08-09 23:53:44 +00:00
);
2009-08-11 22:44:47 +00:00
$this->assertFalse($what->get_queued_transcriptions_for_post(2));
2009-08-09 23:53:44 +00:00
}
2009-08-10 16:50:30 +00:00
function providerTestAddQueuedTranscriptionToPost() {
return array(
array(
array(1, 1, "en", "This is a transcript"), true
),
array(
array(2, 1, "en", "This is a transcript"), false
),
array(
array(1, 2, "en", "This is a transcript"), false
),
array(
array(1, 1, "", "This is a transcript"), false
),
array(
array(1, 1, "en", ""), false
),
);
}
/**
* @dataProvider providerTestAddQueuedTranscriptionToPost
*/
function testAddQueuedTranscriptionToPost($query_settings, $expected_result) {
global $wpdb;
wp_insert_user(array('ID' => 1, 'first_name' => 'Test', 'last_name' => 'User'));
wp_insert_post(array('ID' => 1));
$expected_query = sprintf("INSERT INTO '%s' (post_id, user_id, language, transcript) VALUES ('%d', '%d', '%s', '%s')",
$this->what->table,
1, 1, "en", "This is a transcript");
$wpdb = $this->getMock('wpdb', array('query', 'prepare'));
$wpdb->expects($this->once())
->method('prepare')
->will($this->returnValue($expected_query));
2009-08-11 22:44:47 +00:00
$what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update'));
$what->expects($this->any())
->method('is_user_allowed_to_update')
->will($this->returnValue(true));
2009-08-10 16:50:30 +00:00
if ($expected_result === true) {
$wpdb->expects($this->once())
->method('query')
->with($expected_query)
->will($this->returnValue(true));
}
2009-08-11 22:44:47 +00:00
$this->assertEquals($expected_result, $what->add_queued_transcription_to_post(
2009-08-10 16:50:30 +00:00
1,
array(
'user_id' => 1,
'language' => 'en',
'transcript' => "This is a transcript"
)
));
}
2009-08-11 13:14:37 +00:00
2009-08-11 21:14:46 +00:00
function providerTestIsUserAllowedToUpdate() {
2009-08-11 13:14:37 +00:00
return array(
array(
2009-08-11 21:14:46 +00:00
false, array(), array(), 1, false
2009-08-11 13:14:37 +00:00
),
array(
2009-08-11 21:14:46 +00:00
false, array('edit_posts'), array(), 1, true
2009-08-11 13:14:37 +00:00
),
array(
2009-08-11 21:14:46 +00:00
true, array(), array(2), 1, false
),
2009-08-11 13:14:37 +00:00
array(
2009-08-11 21:14:46 +00:00
true, array(), array(1), 1, true
2009-08-11 13:14:37 +00:00
),
2009-08-11 21:14:46 +00:00
);
}
/**
* @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(
2009-08-11 13:14:37 +00:00
array(
2009-08-11 21:14:46 +00:00
array(), array("language" => "en", "transcript" => "This")
2009-08-11 13:14:37 +00:00
),
array(
2009-08-11 21:14:46 +00:00
array(
(object)array('ID' => 1)
), array("language" => "en", "transcript" => "This")
),
array(
array(
2009-08-11 13:14:37 +00:00
(object)array('ID' => 1)
2009-08-11 21:14:46 +00:00
), array("language" => "en", "transcript" => "This", 'id' => 1)
2009-08-11 13:14:37 +00:00
),
);
}
/**
* @dataProvider providerTestUpdateQueuedTranscription
*/
2009-08-11 21:14:46 +00:00
function testUpdateQueuedTranscription($valid_transcripts, $update_info) {
2009-08-11 13:14:37 +00:00
global $wpdb;
2009-08-11 21:14:46 +00:00
$what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update'));
$what->expects($this->once())
->method('is_user_allowed_to_update')
->will($this->returnValue(true));
2009-08-11 13:14:37 +00:00
$wpdb = $this->getMock('wpdb', array('prepare', 'get_results', 'query'));
2009-08-11 21:14:46 +00:00
$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; }
2009-08-11 13:14:37 +00:00
}
2009-08-11 21:14:46 +00:00
if ($in_array) {
$wpdb->expects($this->once())
2009-08-11 22:16:23 +00:00
->method('query');
2009-08-11 13:14:37 +00:00
}
wp_insert_post(array('ID' => 1));
2009-08-11 22:16:23 +00:00
$what->update_queued_transcription($update_info);
}
function providerTestDeleteQueuedTranscription() {
return array(
array(array(), 1, false),
array(array(2), 1, false),
array(array(1), 1, true)
);
}
/**
* @dataProvider providerTestDeleteQueuedTranscription
*/
function testDeleteQueuedTranscription($valid_transcripts, $transcript_id_to_delete, $expected_result) {
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_var', 'query'));
$wpdb->expects($this->once())
->method('get_var')
->will($this->returnValue(in_array($transcript_id_to_delete, $valid_transcripts) ? $transcript_id_to_delete : null));
if (in_array($transcript_id_to_delete, $valid_transcripts)) {
$wpdb->expects($this->once())
->method('query');
}
$what->delete_queued_transcription($transcript_id_to_delete);
2009-08-11 13:14:37 +00:00
}
2009-08-09 23:53:44 +00:00
}
?>