Merge branch 'develop-ascraeus' into develop

* develop-ascraeus:
  [ticket/11959] Move phpbb_generate_string_list() to functions_content.php.
  [ticket/11959] Add unit tests.
  [ticket/11959] Remove use of plurals and make it possible to use Oxford comma.
  [ticket/11959] Rename phpbb_gen_string_list() & fix incorrect var name.
  [ticket/11959] Simplify how the users are trimmed.
  [ticket/11959] Add function to create a string list.
  [ticket/11959] Add samples for the untrimmed strings.
  [ticket/11959] List the last user with "and" instead of a comma.
  [ticket/11959] Use the plurals system for the untrimmed language strings.
  [ticket/11959] Fix the NOTIFICATION_QUOTE_TRIMMED text.
  [ticket/11959] Use plurals for the language strings.
This commit is contained in:
Joas Schilling 2014-03-29 20:39:33 +01:00
commit d5e4863b3d
7 changed files with 345 additions and 11 deletions

View file

@ -1414,6 +1414,38 @@ function phpbb_add_quickmod_option($option, $lang_string)
)); ));
} }
/**
* Concatenate an array into a string list.
*
* @param array $items Array of items to concatenate
* @param object $user The phpBB $user object.
*
* @return string String list. Examples: "A"; "A and B"; "A, B, and C"
*/
function phpbb_generate_string_list($items, $user)
{
if (empty($items))
{
return '';
}
$count = sizeof($items);
$last_item = array_pop($items);
$lang_key = 'STRING_LIST_MULTI';
if ($count == 1)
{
return $last_item;
}
else if ($count == 2)
{
$lang_key = 'STRING_LIST_SIMPLE';
}
$list = implode($user->lang['COMMA_SEPARATOR'], $items);
return $user->lang($lang_key, $list, $last_item);
}
/** /**
* @package phpBB3 * @package phpBB3
*/ */

View file

@ -422,18 +422,29 @@ $lang = array_merge($lang, array(
'NOT_WATCHING_FORUM' => 'You are no longer subscribed to updates on this forum.', 'NOT_WATCHING_FORUM' => 'You are no longer subscribed to updates on this forum.',
'NOT_WATCHING_TOPIC' => 'You are no longer subscribed to this topic.', 'NOT_WATCHING_TOPIC' => 'You are no longer subscribed to this topic.',
'NOTIFICATIONS' => 'Notifications', 'NOTIFICATIONS' => 'Notifications',
'NOTIFICATION_BOOKMARK' => '%1$s replied to the topic "%2$s" you have bookmarked.', // This applies for NOTIFICATION_BOOKMARK, NOTIFICATION_POST, and NOTIFICATION_QUOTE.
'NOTIFICATION_BOOKMARK_TRIMMED' => '%1$s and %3$d others replied to the topic “%2$s” you have bookmarked.', // %1$s will return a list of users that's concatenated using "," and "and" - see STRING_LIST
// Once the user count reaches 5 users or more, the list is trimmed using NOTIFICATION_X_OTHERS
// Examples:
// A replied...
// A and B replied...
// A, B and C replied...
// A, B, C and 2 others replied...
'NOTIFICATION_BOOKMARK' => array(
1 => '%1$s replied to the topic “%2$s” you have bookmarked.',
),
'NOTIFICATION_GROUP_REQUEST' => '%1$s is requesting to join the group %2$s.', 'NOTIFICATION_GROUP_REQUEST' => '%1$s is requesting to join the group %2$s.',
'NOTIFICATION_GROUP_REQUEST_APPROVED' => 'Your request to join the group %1$s has been approved.', 'NOTIFICATION_GROUP_REQUEST_APPROVED' => 'Your request to join the group %1$s has been approved.',
'NOTIFICATION_PM' => '%1$s sent you a Private Message "%2$s".', 'NOTIFICATION_PM' => '%1$s sent you a Private Message "%2$s".',
'NOTIFICATION_POST' => '%1$s replied to the topic "%2$s".', 'NOTIFICATION_POST' => array(
'NOTIFICATION_POST_TRIMMED' => '%1$s and %3$d others replied to the topic “%2$s”', 1 => '%1$s replied to the topic “%2$s”.',
),
'NOTIFICATION_POST_APPROVED' => 'Your post was approved "%2$s".', 'NOTIFICATION_POST_APPROVED' => 'Your post was approved "%2$s".',
'NOTIFICATION_POST_DISAPPROVED' => 'Your post "%1$s" was disapproved for reason: "%2$s".', 'NOTIFICATION_POST_DISAPPROVED' => 'Your post "%1$s" was disapproved for reason: "%2$s".',
'NOTIFICATION_POST_IN_QUEUE' => 'A new post titled "%2$s" was posted by %1$s and needs approval.', 'NOTIFICATION_POST_IN_QUEUE' => 'A new post titled "%2$s" was posted by %1$s and needs approval.',
'NOTIFICATION_QUOTE' => '%1$s quoted you in the post "%2$s".', 'NOTIFICATION_QUOTE' => array(
'NOTIFICATION_QUOTE_TRIMMED' => '%1$s and %3$d others replied to the topic “%2$s”', 1 => '%1$s quoted you in the post “%2$s”.',
),
'NOTIFICATION_REPORT_PM' => '%1$s reported a Private Message "%2$s" for reason: "%3$s".', 'NOTIFICATION_REPORT_PM' => '%1$s reported a Private Message "%2$s" for reason: "%3$s".',
'NOTIFICATION_REPORT_POST' => '%1$s reported a post "%2$s" for reason: "%3$s".', 'NOTIFICATION_REPORT_POST' => '%1$s reported a post "%2$s" for reason: "%3$s".',
'NOTIFICATION_REPORT_CLOSED' => '%1$s closed the report you made for "%2$s".', 'NOTIFICATION_REPORT_CLOSED' => '%1$s closed the report you made for "%2$s".',
@ -443,6 +454,10 @@ $lang = array_merge($lang, array(
'NOTIFICATION_TOPIC_IN_QUEUE' => 'A new topic titled "%2$s" was posted by %1$s and needs approval.', 'NOTIFICATION_TOPIC_IN_QUEUE' => 'A new topic titled "%2$s" was posted by %1$s and needs approval.',
'NOTIFICATION_TYPE_NOT_EXIST' => 'The notification type "%s" is missing from the file system.', 'NOTIFICATION_TYPE_NOT_EXIST' => 'The notification type "%s" is missing from the file system.',
'NOTIFICATION_ADMIN_ACTIVATE_USER' => 'The user “%1$s” is newly registered and requires activation.', 'NOTIFICATION_ADMIN_ACTIVATE_USER' => 'The user “%1$s” is newly registered and requires activation.',
// Used in conjuction with NOTIFICATION_BOOKMARK, NOTIFICATION_POST, and NOTIFICATION_QUOTE.
'NOTIFICATION_X_OTHERS' => array(
2 => '%d others',
),
'NOTIFY_ADMIN' => 'Please notify the board administrator or webmaster.', 'NOTIFY_ADMIN' => 'Please notify the board administrator or webmaster.',
'NOTIFY_ADMIN_EMAIL' => 'Please notify the board administrator or webmaster: <a href="mailto:%1$s">%1$s</a>', 'NOTIFY_ADMIN_EMAIL' => 'Please notify the board administrator or webmaster: <a href="mailto:%1$s">%1$s</a>',
'NO_ACCESS_ATTACHMENT' => 'You are not allowed to access this file.', 'NO_ACCESS_ATTACHMENT' => 'You are not allowed to access this file.',
@ -666,6 +681,8 @@ $lang = array_merge($lang, array(
'START_WATCHING_TOPIC' => 'Subscribe topic', 'START_WATCHING_TOPIC' => 'Subscribe topic',
'STOP_WATCHING_FORUM' => 'Unsubscribe forum', 'STOP_WATCHING_FORUM' => 'Unsubscribe forum',
'STOP_WATCHING_TOPIC' => 'Unsubscribe topic', 'STOP_WATCHING_TOPIC' => 'Unsubscribe topic',
'STRING_LIST_MULTI' => '%1$s, and %2$s',
'STRING_LIST_SIMPLE' => '%1$s and %2$s',
'SUBFORUM' => 'Subforum', 'SUBFORUM' => 'Subforum',
'SUBFORUMS' => 'Subforums', 'SUBFORUMS' => 'Subforums',
'SUBJECT' => 'Subject', 'SUBJECT' => 'Subject',

View file

@ -205,18 +205,17 @@ class post extends \phpbb\notification\type\base
$usernames[] = $this->user_loader->get_username($responder['poster_id'], 'no_profile'); $usernames[] = $this->user_loader->get_username($responder['poster_id'], 'no_profile');
} }
} }
$lang_key = $this->language_key;
if ($trimmed_responders_cnt) if ($trimmed_responders_cnt)
{ {
$lang_key .= '_TRIMMED'; $usernames[] = $this->user->lang('NOTIFICATION_X_OTHERS', $trimmed_responders_cnt);
} }
return $this->user->lang( return $this->user->lang(
$lang_key, $this->language_key,
implode($this->user->lang['COMMA_SEPARATOR'], $usernames), phpbb_generate_string_list($usernames, $this->user),
censor_text($this->get_data('topic_title')), censor_text($this->get_data('topic_title')),
$trimmed_responders_cnt $responders_cnt
); );
} }

View file

@ -0,0 +1,60 @@
<?php
/**
*
* @package testing
* @copyright (c) 2014 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_generate_string_list_test extends phpbb_test_case
{
public $user;
public function setUp()
{
parent::setUp();
$this->user = new \phpbb\user();
$this->user->data = array('user_lang' => 'en');
$this->user->add_lang('common');
}
public function generate_string_list_data()
{
return array(
array(
array(),
'',
),
array(
array('A'),
'A',
),
array(
array(2 => 'A', 3 => 'B'),
'A and B',
),
array(
array('A' => 'A', 'B' => 'B', 'C' => 'C'),
'A, B, and C',
),
array(
array('A', 'B', 'C', 'D'),
'A, B, C, and D',
)
);
}
/**
* @dataProvider generate_string_list_data
*/
public function test_generate_string_list($items, $expected_result)
{
$result = phpbb_generate_string_list($items, $this->user);
$this->assertEquals($expected_result, $result);
}
}

View file

@ -0,0 +1,36 @@
<?php
/**
*
* @package notifications
* @copyright (c) 2014 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
class phpbb_mock_notification_type_post extends \phpbb\notification\type\post
{
public function __construct($user_loader, $db, $cache, $user, $auth, $config, $phpbb_root_path, $php_ext, $notification_types_table, $notifications_table, $user_notifications_table)
{
$this->user_loader = $user_loader;
$this->db = $db;
$this->cache = $cache;
$this->user = $user;
$this->auth = $auth;
$this->config = $config;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->notification_types_table = $notification_types_table;
$this->notifications_table = $notifications_table;
$this->user_notifications_table = $user_notifications_table;
}
}

View file

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<table name="phpbb_users">
<column>user_id</column>
<column>username</column>
<column>username_clean</column>
<column>user_colour</column>
<column>user_permissions</column>
<column>user_sig</column>
<row>
<value>2</value>
<value>A</value>
<value>a</value>
<value></value>
<value></value>
<value></value>
</row>
<row>
<value>3</value>
<value>B</value>
<value>b</value>
<value></value>
<value></value>
<value></value>
</row>
<row>
<value>4</value>
<value>C</value>
<value>c</value>
<value></value>
<value></value>
<value></value>
</row>
<row>
<value>5</value>
<value>D</value>
<value>d</value>
<value></value>
<value></value>
<value></value>
</row>
<row>
<value>6</value>
<value>E</value>
<value>e</value>
<value></value>
<value></value>
<value></value>
</row>
</table>
</dataset>

View file

@ -0,0 +1,139 @@
<?php
/**
*
* @package testing
* @copyright (c) 2014 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_notification_user_list_trim_test extends phpbb_database_test_case
{
protected $notification;
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/user_list_trim.xml');
}
public function setUp()
{
global $phpbb_root_path, $phpEx, $phpbb_dispatcher, $user, $cache, $auth;
parent::setUp();
$phpbb_dispatcher = new phpbb_mock_event_dispatcher();
$db = $this->new_dbal();
$config = new \phpbb\config\config(array());
set_config(null, null, null, $config);
set_config_count(null, null, null, $config);
$cache = new \phpbb\cache\service(
new \phpbb\cache\driver\null(),
$config,
$db,
$phpbb_root_path,
$phpEx
);
$auth = $this->getMock('\phpbb\auth\auth');
$auth->expects($this->any())
->method('acl_get')
->with($this->stringContains('_'),
$this->anything())
->will($this->returnValueMap(array(
array('u_viewprofile', 1, false),
)));
$user = new \phpbb\user();
$user->data = array('user_lang' => 'en');
$user->add_lang('common');
$user_loader = new phpbb\user_loader($db, $phpbb_root_path, $phpEx, USERS_TABLE);
$user_loader->load_users(array(2, 3, 4, 5, 6));
$this->notification = new phpbb_mock_notification_type_post(
$user_loader, null, null, $user, null, null, $phpbb_root_path, $phpEx, null, null, null
);
}
public function user_list_trim_data()
{
return array(
array(
array(
'topic_title' => 'Test',
'poster_id' => 2,
'post_username' => 'A',
'responders' => null,
),
'A replied to the topic “Test”.',
),
array(
array(
'topic_title' => 'Test',
'poster_id' => 2,
'post_username' => 'A',
'responders' => array(
array('username' => '', 'poster_id' => 3),
),
),
'A and B replied to the topic “Test”.',
),
array(
array(
'topic_title' => 'Test',
'poster_id' => 2,
'post_username' => 'A',
'responders' => array(
array('username' => '', 'poster_id' => 3),
array('username' => '', 'poster_id' => 4),
),
),
'A, B, and C replied to the topic “Test”.',
),
array(
array(
'topic_title' => 'Test',
'poster_id' => 2,
'post_username' => 'A',
'responders' => array(
array('username' => '', 'poster_id' => 3),
array('username' => '', 'poster_id' => 4),
array('username' => '', 'poster_id' => 5),
),
),
'A, B, C, and D replied to the topic “Test”.',
),
array(
array(
'topic_title' => 'Test',
'poster_id' => 2,
'post_username' => 'A',
'responders' => array(
array('username' => '', 'poster_id' => 3),
array('username' => '', 'poster_id' => 4),
array('username' => '', 'poster_id' => 5),
array('username' => '', 'poster_id' => 6),
),
),
'A, B, C, and 2 others replied to the topic “Test”.',
),
);
}
/**
* @dataProvider user_list_trim_data
*/
public function test_user_list_trim($data, $expected_result)
{
$data = array('notification_data' => serialize($data));
$this->notification->set_initial_data($data);
$this->assertEquals($expected_result, $this->notification->get_title());
}
}