Skip to content

Commit 40d0c91

Browse files
committed
Validate language before setting it
1 parent 577131f commit 40d0c91

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

entity/rule.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,28 @@ public function get_language()
535535
* @param string $language language iso
536536
* @return rule_interface $this object for chaining calls; load()->set()->save()
537537
* @access public
538+
* @throws \phpbb\boardrules\exception\unexpected_value
538539
*/
539540
public function set_language($language)
540541
{
541542
if (!isset($this->data['rule_language']))
542543
{
544+
// Validate the requested language ISO is installed
545+
if ($language !== '')
546+
{
547+
$sql = 'SELECT lang_id
548+
FROM ' . LANG_TABLE . "
549+
WHERE lang_iso = '" . $this->db->sql_escape($language) . "'";
550+
$result = $this->db->sql_query($sql);
551+
$lang_id = $this->db->sql_fetchfield('lang_id');
552+
$this->db->sql_freeresult($result);
553+
554+
if (!$lang_id)
555+
{
556+
throw new \phpbb\boardrules\exception\unexpected_value(array('rule_language', 'WRONG_DATA_LANG', 'UNEXPECTED_VALUE'));
557+
}
558+
}
559+
543560
$this->data['rule_language'] = $language;
544561
}
545562

tests/entity/fixtures/rule.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,16 @@
7070
<value>7</value>
7171
</row>
7272
</table>
73+
<table name="phpbb_lang">
74+
<column>lang_id</column>
75+
<column>lang_iso</column>
76+
<row>
77+
<value>1</value>
78+
<value>en</value>
79+
</row>
80+
<row>
81+
<value>2</value>
82+
<value>en-us</value>
83+
</row>
84+
</table>
7385
</dataset>

tests/entity/rule_entity_language_test.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ public function test_set_language($data, $language)
7575
// Setup the entity class
7676
$entity = $this->get_rule_entity();
7777

78+
// Assert that the anchor matches what's expected
79+
if ($language !== 'en')
80+
{
81+
$this->setExpectedException('\phpbb\boardrules\exception\unexpected_value');
82+
}
83+
7884
// Set the anchor
7985
$entity->set_language($language);
8086

0 commit comments

Comments
 (0)