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

268 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;
_reset_wp();
$wpdb = null;
2009-08-19 00:21:21 +00:00
_set_user_capabilities('submit_transcriptions', 'approve_transcriptions');
2009-08-09 23:53:44 +00:00
}
function testSaveTranscription() {
2009-08-11 21:14:46 +00:00
wp_insert_post(array('ID' => 1));
2009-08-19 00:21:21 +00:00
2009-08-11 22:44:47 +00:00
$what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update'));
$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() {
2009-08-12 23:12:52 +00:00
$what = new WhatDidTheySay();
2009-08-09 23:53:44 +00:00
update_post_meta(1, "provided_transcripts", array('en' => 'this is a new transcript', 'fr' => "il s'agit d'une nouvelle transcription"));
2009-08-12 23:12:52 +00:00
$this->assertEquals(array('en', 'fr'), $what->get_transcript_languages(1));
2009-08-09 23:53:44 +00:00
2009-08-12 23:12:52 +00:00
$this->assertEquals(false, $what->get_transcript_languages(2));
2009-08-09 23:53:44 +00:00
}
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'));
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'));
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 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'));
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'));
$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-12 23:02:36 +00:00
function providerTestAddTranscriptionToPost() {
return array(
array(null, false),
array((object)array('id' => 1, 'post_id' => 2), false),
array((object)array('id' => 1, 'post_id' => 1, 'language' => 'en', 'transcript' => 'This is a transcript'), true)
);
}
/**
* @dataProvider providerTestAddTranscriptionToPost
*/
function testAddTranscriptionToPost($get_results_return, $expects_delete) {
global $wpdb;
$what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update', 'save_transcript'));
wp_insert_post((object)array('ID' => 1));
$wpdb = $this->getMock('wpdb', array('get_results', 'prepare', 'query'));
$wpdb->expects($this->once())
->method('get_results')
->will($this->returnValue(array($get_results_return)));
if ($expects_delete) {
$wpdb->expects($this->at(2))
->method('prepare')
->with(new PHPUnit_Framework_Constraint_PCREMatch('#DELETE FROM .* WHERE id = .*#'));
$what->expects($this->once())
->method('save_transcript');
}
$what->add_transcription_to_post(1);
}
2009-08-12 23:12:52 +00:00
function testDeleteTranscript() {
$what = $this->getMock('WhatDidTheySay', array('is_user_allowed_to_update'));
wp_insert_post((object)array('ID' => 1));
update_post_meta(1, "provided_transcripts", array("en" => "This is a transcript"));
$what->delete_transcript(1, "en");
$this->assertEquals(array(), get_post_meta(1, "provided_transcripts", true));
}
2009-08-09 23:53:44 +00:00
}
?>