From b3f3886cd90f5d675e552455d2e6eb7b2143972c Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 30 Dec 2016 15:13:47 +0100 Subject: [PATCH] [ticket/13867] Add tests PHPBB3-13867 --- phpBB/includes/acp/acp_profile.php | 4 + phpBB/phpbb/profilefields/manager.php | 2 +- tests/profilefields/fixtures/manager.xml | 31 +++++ tests/profilefields/manager_test.php | 137 +++++++++++++++++++++++ tests/test_framework/phpbb_test_case.php | 1 + 5 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 tests/profilefields/fixtures/manager.xml create mode 100644 tests/profilefields/manager_test.php diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index 8164c07704..49da7d84a4 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -783,6 +783,10 @@ class acp_profile $s_one_need_edit = true; } + if (!isset($this->type_collection[$row['field_type']])) + { + continue; + } $profile_field = $this->type_collection[$row['field_type']]; $field_block = array( diff --git a/phpBB/phpbb/profilefields/manager.php b/phpBB/phpbb/profilefields/manager.php index 9c8876e6bc..d462d6d4f7 100644 --- a/phpBB/phpbb/profilefields/manager.php +++ b/phpBB/phpbb/profilefields/manager.php @@ -665,7 +665,7 @@ class manager * @param bool $active True to limit output to active profile fields, false for all * @return array Array with profile field ids as keys and idents as values */ - private function list_profilefields($profilefield_type_name, $active=false) + private function list_profilefields($profilefield_type_name, $active = false) { // Get list of profile fields affected by this operation, if any $profile_fields = array(); diff --git a/tests/profilefields/fixtures/manager.xml b/tests/profilefields/fixtures/manager.xml new file mode 100644 index 0000000000..b0b118bb13 --- /dev/null +++ b/tests/profilefields/fixtures/manager.xml @@ -0,0 +1,31 @@ + + + + field_id + field_ident + field_active + field_type + field_order + + 1 + pf_1 + 1 + foo_bar_type + 1 + + + 2 + pf_2 + 1 + foo_bar_type + 2 + + + 3 + pf_3 + 1 + other_type + 3 + +
+
diff --git a/tests/profilefields/manager_test.php b/tests/profilefields/manager_test.php new file mode 100644 index 0000000000..76bc63124f --- /dev/null +++ b/tests/profilefields/manager_test.php @@ -0,0 +1,137 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +class manager_test extends phpbb_database_test_case +{ + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** @var \phpbb\config\db_text */ + protected $config_text; + + /** @var \phpbb\profilefields\manager */ + protected $manager; + + /** @var \phpbb\log\log_interface */ + protected $log; + + /** @var \phpbb\db\tools */ + protected $db_tools; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/manager.xml'); + } + + public function setUp() + { + parent::setUp(); + + global $phpbb_log; + + $this->log = $this->prophesize('\phpbb\log\log_interface'); + $phpbb_log = $this->log->reveal(); + + $this->db = $this->new_dbal(); + $this->config_text = $this->prophesize('\phpbb\config\db_text'); + $this->db_tools = $this->prophesize('\phpbb\db\tools'); + + $this->manager = new \phpbb\profilefields\manager( + $this->prophesize('phpbb\auth\auth')->reveal(), + $this->db, + $this->db_tools->reveal(), + $this->prophesize('\phpbb\event\dispatcher_interface')->reveal(), + $this->prophesize('\phpbb\request\request')->reveal(), + $this->prophesize('\phpbb\template\template')->reveal(), + $this->prophesize('\phpbb\di\service_collection')->reveal(), + $this->prophesize('\phpbb\user')->reveal(), + $this->config_text->reveal(), + PROFILE_FIELDS_TABLE, + PROFILE_FIELDS_LANG_TABLE, + PROFILE_FIELDS_DATA_TABLE + ); + } + + public function test_disable_profilefields() + { + $this->log->add('admin', 'LOG_PROFILE_FIELD_DEACTIVATE', 'pf_1')->shouldBeCalled(); + $this->log->add('admin', 'LOG_PROFILE_FIELD_DEACTIVATE', 'pf_2')->shouldBeCalled(); + + $this->config_text->set('foo_bar_type.saved', json_encode([1 => 'pf_1', 2 => 'pf_2']))->shouldBeCalled(); + + $this->manager->disable_profilefields('foo_bar_type'); + + $sql = 'SELECT field_id, field_ident + FROM ' . PROFILE_FIELDS_TABLE . " + WHERE field_active = 1 + AND field_type = 'foo_bar_type'"; + + $this->assertSqlResultEquals(false, $sql, 'All profile fields should be disabled'); + } + + public function test_enable_profilefields() + { + $this->log->add('admin', 'LOG_PROFILE_FIELD_ACTIVATE', 'pf_1')->shouldBeCalled(); + $this->log->add('admin', 'LOG_PROFILE_FIELD_ACTIVATE', 'pf_2')->shouldBeCalled(); + + $this->config_text->get('foo_bar_type.saved')->willReturn(json_encode([1 => 'pf_1', 2 => 'pf_2'])); + $this->config_text->delete('foo_bar_type.saved')->shouldBeCalled(); + + $this->manager->enable_profilefields('foo_bar_type'); + + $sql = 'SELECT field_id + FROM ' . PROFILE_FIELDS_TABLE . " + WHERE field_active = 1 + AND field_type = 'foo_bar_type'"; + + $this->assertSqlResultEquals([ + ['field_id' => '1'], + ['field_id' => '2'], + ], $sql, 'All profile fields should be enabled'); + } + + public function test_purge_profilefields() + { + $this->log->add('admin', 'LOG_PROFILE_FIELD_REMOVED', 'pf_1')->shouldBeCalled(); + $this->log->add('admin', 'LOG_PROFILE_FIELD_REMOVED', 'pf_2')->shouldBeCalled(); + + $this->config_text->delete('foo_bar_type.saved')->shouldBeCalled(); + + $this->db_tools->sql_column_remove(PROFILE_FIELDS_DATA_TABLE, 'pf_pf_1')->shouldBeCalled(); + $this->db_tools->sql_column_remove(PROFILE_FIELDS_DATA_TABLE, 'pf_pf_2')->shouldBeCalled(); + + $this->manager->enable_profilefields('foo_bar_type'); + + $sql = 'SELECT field_id + FROM ' . PROFILE_FIELDS_TABLE . " + WHERE field_type = 'foo_bar_type'"; + + $this->assertSqlResultEquals(false, $sql, 'All profile fields should be removed'); + + $sql = 'SELECT field_id + FROM ' . PROFILE_FIELDS_LANG_TABLE . " + WHERE field_type = 'foo_bar_type'"; + + $this->assertSqlResultEquals(false, $sql, 'All profile fields lang should be removed'); + + $sql = 'SELECT field_id + FROM ' . PROFILE_LANG_TABLE . " + WHERE field_type = 'foo_bar_type'"; + + $this->assertSqlResultEquals(false, $sql, 'All profile fields lang should be removed'); + + $sql = 'SELECT field_id, field_order FROM ' . PROFILE_FIELDS_TABLE; + + $this->assertSqlResultEquals([['field_id' => '3', 'field_order' => '1'],], $sql, 'Profile fields order should be recalculated, starting by 1'); + } +} diff --git a/tests/test_framework/phpbb_test_case.php b/tests/test_framework/phpbb_test_case.php index 01d26fb67d..0bf464567f 100644 --- a/tests/test_framework/phpbb_test_case.php +++ b/tests/test_framework/phpbb_test_case.php @@ -27,6 +27,7 @@ class phpbb_test_case extends PHPUnit_Framework_TestCase 'PHP_Token_Stream_CachingFactory' => array('cache'), 'phpbb_database_test_case' => array('already_connected', 'last_post_timestamp'), + 'Prophecy\Doubler\NameGenerator' => array('counter'), ); }