Merge pull request #3333 from marc1706/ticket/13522

[ticket/13522] Remove empty answers from possible answers in Q&A
This commit is contained in:
Joas Schilling 2015-04-06 11:04:35 +02:00
commit 009a8cb2d5
2 changed files with 125 additions and 31 deletions

View file

@ -125,7 +125,7 @@ class qa
*/ */
public function is_available() public function is_available()
{ {
global $config, $db, $phpbb_root_path, $phpEx, $user; global $config, $db, $user;
// load language file for pretty display in the ACP dropdown // load language file for pretty display in the ACP dropdown
$user->add_lang('captcha_qa'); $user->add_lang('captcha_qa');
@ -263,7 +263,7 @@ class qa
*/ */
function garbage_collect($type = 0) function garbage_collect($type = 0)
{ {
global $db, $config; global $db;
$sql = 'SELECT c.confirm_id $sql = 'SELECT c.confirm_id
FROM ' . $this->table_qa_confirm . ' c FROM ' . $this->table_qa_confirm . ' c
@ -310,8 +310,6 @@ class qa
$db_tool = new \phpbb\db\tools($db); $db_tool = new \phpbb\db\tools($db);
$tables = array($this->table_captcha_questions, $this->table_captcha_answers, $this->table_qa_confirm);
$schemas = array( $schemas = array(
$this->table_captcha_questions => array ( $this->table_captcha_questions => array (
'COLUMNS' => array( 'COLUMNS' => array(
@ -366,7 +364,7 @@ class qa
*/ */
function validate() function validate()
{ {
global $config, $db, $user; global $user;
$error = ''; $error = '';
@ -414,7 +412,7 @@ class qa
if (!sizeof($this->question_ids)) if (!sizeof($this->question_ids))
{ {
return false; return;
} }
$this->confirm_id = md5(unique_id($user->ip)); $this->confirm_id = md5(unique_id($user->ip));
$this->question = (int) array_rand($this->question_ids); $this->question = (int) array_rand($this->question_ids);
@ -440,7 +438,7 @@ class qa
if (!sizeof($this->question_ids)) if (!sizeof($this->question_ids))
{ {
return false; return;
} }
$this->question = (int) array_rand($this->question_ids); $this->question = (int) array_rand($this->question_ids);
@ -611,8 +609,8 @@ class qa
*/ */
function acp_page($id, &$module) function acp_page($id, &$module)
{ {
global $db, $user, $auth, $template; global $user, $template;
global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx; global $config;
$user->add_lang('acp/board'); $user->add_lang('acp/board');
$user->add_lang('captcha_qa'); $user->add_lang('captcha_qa');
@ -674,11 +672,7 @@ class qa
else else
{ {
// okay, show the editor // okay, show the editor
$error = false; $question_input = $this->acp_get_question_input();
$input_question = request_var('question_text', '', true);
$input_answers = request_var('answers', '', true);
$input_lang = request_var('lang_iso', '', true);
$input_strict = request_var('strict', false);
$langs = $this->get_languages(); $langs = $this->get_languages();
foreach ($langs as $lang => $entry) foreach ($langs as $lang => $entry)
@ -697,13 +691,11 @@ class qa
{ {
if ($question = $this->acp_get_question_data($question_id)) if ($question = $this->acp_get_question_data($question_id))
{ {
$answers = (isset($input_answers[$lang])) ? $input_answers[$lang] : implode("\n", $question['answers']);
$template->assign_vars(array( $template->assign_vars(array(
'QUESTION_TEXT' => ($input_question) ? $input_question : $question['question_text'], 'QUESTION_TEXT' => ($question_input['question_text']) ? $question_input['question_text'] : $question['question_text'],
'LANG_ISO' => ($input_lang) ? $input_lang : $question['lang_iso'], 'LANG_ISO' => ($question_input['lang_iso']) ? $question_input['lang_iso'] : $question['lang_iso'],
'STRICT' => (isset($_REQUEST['strict'])) ? $input_strict : $question['strict'], 'STRICT' => (isset($_REQUEST['strict'])) ? $question_input['strict'] : $question['strict'],
'ANSWERS' => $answers, 'ANSWERS' => implode("\n", $question['answers']),
)); ));
} }
else else
@ -714,18 +706,16 @@ class qa
else else
{ {
$template->assign_vars(array( $template->assign_vars(array(
'QUESTION_TEXT' => $input_question, 'QUESTION_TEXT' => $question_input['question_text'],
'LANG_ISO' => $input_lang, 'LANG_ISO' => $question_input['lang_iso'],
'STRICT' => $input_strict, 'STRICT' => $question_input['strict'],
'ANSWERS' => $input_answers, 'ANSWERS' => (is_array($question_input['answers'])) ? implode("\n", $question_input['answers']) : '',
)); ));
} }
if ($submit && check_form_key($form_key)) if ($submit && check_form_key($form_key))
{ {
$data = $this->acp_get_question_input(); if (!$this->validate_input($question_input))
if (!$this->validate_input($data))
{ {
$template->assign_vars(array( $template->assign_vars(array(
'S_ERROR' => true, 'S_ERROR' => true,
@ -735,11 +725,11 @@ class qa
{ {
if ($question_id) if ($question_id)
{ {
$this->acp_update_question($data, $question_id); $this->acp_update_question($question_input, $question_id);
} }
else else
{ {
$this->acp_add_question($data); $this->acp_add_question($question_input);
} }
add_log('admin', 'LOG_CONFIG_VISUAL'); add_log('admin', 'LOG_CONFIG_VISUAL');
@ -819,6 +809,8 @@ class qa
return $question; return $question;
} }
return false;
} }
/** /**
@ -827,13 +819,21 @@ class qa
function acp_get_question_input() function acp_get_question_input()
{ {
$answers = utf8_normalize_nfc(request_var('answers', '', true)); $answers = utf8_normalize_nfc(request_var('answers', '', true));
// Convert answers into array and filter if answers are set
if (strlen($answers))
{
$answers = array_filter(array_map('trim', explode("\n", $answers)), function ($value) {
return $value !== '';
});
}
$question = array( $question = array(
'question_text' => request_var('question_text', '', true), 'question_text' => request_var('question_text', '', true),
'strict' => request_var('strict', false), 'strict' => request_var('strict', false),
'lang_iso' => request_var('lang_iso', ''), 'lang_iso' => request_var('lang_iso', ''),
'answers' => (strlen($answers)) ? explode("\n", $answers) : '', 'answers' => $answers,
); );
return $question; return $question;
} }

94
tests/captcha/qa_test.php Normal file
View file

@ -0,0 +1,94 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_captcha_qa_test extends \phpbb_database_test_case
{
protected $request;
/** @var \phpbb\captcha\plugins\qa */
protected $qa;
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/../fixtures/empty.xml');
}
public function setUp()
{
global $db;
$db = $this->new_dbal();
parent::setUp();
$this->request = new \phpbb_mock_request();
request_var(false, false, false, false, $this->request);
$this->qa = new \phpbb\captcha\plugins\qa('phpbb_captcha_questions', 'phpbb_captcha_answers', 'phpbb_qa_confirm');
}
public function test_is_installed()
{
$this->assertFalse($this->qa->is_installed());
$this->qa->install();
$this->assertTrue($this->qa->is_installed());
}
public function test_set_get_name()
{
$this->assertNull($this->qa->get_service_name());
$this->qa->set_name('foobar');
$this->assertSame('foobar', $this->qa->get_service_name());
}
public function data_acp_get_question_input()
{
return array(
array("foobar\ntest\nyes", array(
'question_text' => '',
'strict' => false,
'lang_iso' => '',
'answers' => array('foobar', 'test', 'yes')
)),
array("foobar\ntest\n \nyes", array(
'question_text' => '',
'strict' => false,
'lang_iso' => '',
'answers' => array(
0 => 'foobar',
1 => 'test',
3 => 'yes',
)
)),
array('', array(
'question_text' => '',
'strict' => false,
'lang_iso' => '',
'answers' => '',
)),
);
}
/**
* @dataProvider data_acp_get_question_input
*/
public function test_acp_get_question_input($value, $expected)
{
$this->request->overwrite('answers', $value);
$this->assertEquals($expected, $this->qa->acp_get_question_input());
}
}