From 045c0e4d8f60b8ecfc105ba38ea152db6f5d673d Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 3 Mar 2013 19:10:09 +0100 Subject: [PATCH] MDL-38298 Take out harcoded dependency + tests. With that change the filte_emoticon will be easily resusable by other "emoticon filters". --- filter/emoticon/filter.php | 2 +- filter/emoticon/tests/filter_test.php | 74 +++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 filter/emoticon/tests/filter_test.php diff --git a/filter/emoticon/filter.php b/filter/emoticon/filter.php index b312c9e9176..b7b68b2d671 100644 --- a/filter/emoticon/filter.php +++ b/filter/emoticon/filter.php @@ -96,7 +96,7 @@ class filter_emoticon extends moodle_text_filter { */ protected function load_global_config() { if (is_null(self::$globalconfig)) { - self::$globalconfig = get_config('filter_emoticon'); + self::$globalconfig = get_config(get_class($this)); } } diff --git a/filter/emoticon/tests/filter_test.php b/filter/emoticon/tests/filter_test.php new file mode 100644 index 00000000000..ddd73e11587 --- /dev/null +++ b/filter/emoticon/tests/filter_test.php @@ -0,0 +1,74 @@ +. + +/** + * Skype icons filter phpunit tests + * + * @package filter_emoticon + * @category test + * @copyright 2013 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/filter/emoticon/filter.php'); // Include the code to test. + +/** + * Skype icons filter testcase. + */ +class filter_emoticon_testcase extends advanced_testcase { + + /** + * Verify configured target formats are observed. Just that. + */ + public function test_filter_emoticon_formats() { + + $this->resetAfterTest(true); // We are modifying the config. + + $filter = new testable_filter_emoticon(); + + // Verify texts not matching target formats aren't filtered. + $expected = '(grr)'; + $options = array('originalformat' => FORMAT_MOODLE); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}. + $this->assertEquals($expected, $filter->filter('(grr)', $options)); + + $options = array('originalformat' => FORMAT_MARKDOWN); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}. + $this->assertEquals($expected, $filter->filter('(grr)', $options)); + + $options = array('originalformat' => FORMAT_PLAIN); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}. + $this->assertEquals($expected, $filter->filter('(grr)', $options)); + + // And texts matching target formats are filtered. + $expected = 'angry'; + $options = array('originalformat' => FORMAT_HTML); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}. + $this->assertEquals($expected, $filter->filter('(grr)', $options)); + } +} + +/** + * Subclass for easier testing. + */ +class testable_filter_emoticon extends filter_emoticon { + public function __construct() { + // Use this context for filtering. + $this->context = context_system::instance(); + // Define FORMAT_HTML as only one filtering in DB. + set_config('formats', implode(',', array(FORMAT_HTML)), get_class($this)); + } +}