function testURIDefinitionValidation() { $parser = new HTMLPurifier_URIParser(); $uri = $parser->parse('http://example.com'); $this->config->set('URI.DefinitionID', 'HTMLPurifier_AttrDef_URITest->testURIDefinitionValidation'); generate_mock_once('HTMLPurifier_URIDefinition'); $uri_def = new HTMLPurifier_URIDefinitionMock(); $uri_def->expectOnce('filter', array($uri, '*', '*')); $uri_def->setReturnValue('filter', true, array($uri, '*', '*')); $uri_def->expectOnce('postFilter', array($uri, '*', '*')); $uri_def->setReturnValue('postFilter', true, array($uri, '*', '*')); $uri_def->setup = true; // Since definitions are no longer passed by reference, we need // to muck around with the cache to insert our mock. This is // technically a little bad, since the cache shouldn't change // behavior, but I don't feel too good about letting users // overload entire definitions. generate_mock_once('HTMLPurifier_DefinitionCache'); $cache_mock = new HTMLPurifier_DefinitionCacheMock(); $cache_mock->setReturnValue('get', $uri_def); generate_mock_once('HTMLPurifier_DefinitionCacheFactory'); $factory_mock = new HTMLPurifier_DefinitionCacheFactoryMock(); $old = HTMLPurifier_DefinitionCacheFactory::instance(); HTMLPurifier_DefinitionCacheFactory::instance($factory_mock); $factory_mock->setReturnValue('create', $cache_mock); $this->assertDef('http://example.com'); HTMLPurifier_DefinitionCacheFactory::instance($old); }
function test() { generate_mock_once('HTMLPurifier_URIScheme'); $config = HTMLPurifier_Config::create(array('URI.AllowedSchemes' => 'http, telnet', 'URI.OverrideAllowedSchemes' => true)); $context = new HTMLPurifier_Context(); $registry = new HTMLPurifier_URISchemeRegistry(); $this->assertIsA($registry->getScheme('http', $config, $context), 'HTMLPurifier_URIScheme_http'); $scheme_http = new HTMLPurifier_URISchemeMock(); $scheme_telnet = new HTMLPurifier_URISchemeMock(); $scheme_foobar = new HTMLPurifier_URISchemeMock(); // register a new scheme $registry->register('telnet', $scheme_telnet); $this->assertIdentical($registry->getScheme('telnet', $config, $context), $scheme_telnet); // overload a scheme, this is FINAL (forget about defaults) $registry->register('http', $scheme_http); $this->assertIdentical($registry->getScheme('http', $config, $context), $scheme_http); // when we register a scheme, it's automatically allowed $registry->register('foobar', $scheme_foobar); $this->assertIdentical($registry->getScheme('foobar', $config, $context), $scheme_foobar); // now, test when overriding is not allowed $config = HTMLPurifier_Config::create(array('URI.AllowedSchemes' => 'http, telnet', 'URI.OverrideAllowedSchemes' => false)); $this->assertNull($registry->getScheme('foobar', $config, $context)); // scheme not allowed and never registered $this->assertNull($registry->getScheme('ftp', $config, $context)); }
public function test() { generate_mock_once('HTMLPurifier_Strategy'); generate_mock_once('HTMLPurifier_Config'); generate_mock_once('HTMLPurifier_Context'); // setup a bunch of mock strategies to inject into our composite test $mock_1 = new HTMLPurifier_StrategyMock(); $mock_2 = new HTMLPurifier_StrategyMock(); $mock_3 = new HTMLPurifier_StrategyMock(); // setup the object $strategies = array(&$mock_1, &$mock_2, &$mock_3); $composite = new HTMLPurifier_Strategy_Composite_Test($strategies); // setup expectations $input_1 = 'This is raw data'; $input_2 = 'Processed by 1'; $input_3 = 'Processed by 1 and 2'; $input_4 = 'Processed by 1, 2 and 3'; // expected output $config = new HTMLPurifier_ConfigMock(); $context = new HTMLPurifier_ContextMock(); $params_1 = array($input_1, $config, $context); $params_2 = array($input_2, $config, $context); $params_3 = array($input_3, $config, $context); $mock_1->expectOnce('execute', $params_1); $mock_1->setReturnValue('execute', $input_2, $params_1); $mock_2->expectOnce('execute', $params_2); $mock_2->setReturnValue('execute', $input_3, $params_2); $mock_3->expectOnce('execute', $params_3); $mock_3->setReturnValue('execute', $input_4, $params_3); // perform test $output = $composite->execute($input_1, $config, $context); $this->assertIdentical($input_4, $output); }
public function setUp() { parent::setUp(); generate_mock_once('HTMLPurifier_AttrDef'); $this->with = new HTMLPurifier_AttrDefMock(); $this->without = new HTMLPurifier_AttrDefMock(); $this->def = new HTMLPurifier_AttrDef_Switch('tag', $this->with, $this->without); }
protected function &setUpSchemeRegistryMock() { $this->oldRegistry = HTMLPurifier_URISchemeRegistry::instance(); generate_mock_once('HTMLPurifier_URIScheme'); generate_mock_once('HTMLPurifier_URISchemeRegistry'); $registry = HTMLPurifier_URISchemeRegistry::instance(new HTMLPurifier_URISchemeRegistryMock()); return $registry; }
/** * Generate a configuration mock object that returns $values * to a getBatch() call * @param $values Values to return when getBatch is invoked */ protected function generateConfigMock($serial = 'defaultserial') { generate_mock_once('HTMLPurifier_Config'); $config = new HTMLPurifier_ConfigMock(); $config->setReturnValue('getBatchSerial', $serial, array('Test')); $config->version = '1.0.0'; return $config; }
public function test_register() { generate_mock_once('HTMLPurifier_DefinitionCache'); $this->config->set('Cache.DefinitionImpl', 'TestCache'); $this->factory->register('TestCache', $class = 'HTMLPurifier_DefinitionCacheMock'); $cache = $this->factory->create('Test', $this->config); $this->assertIsA($cache, $class); }
public function setUp() { parent::setUp(); $this->obj = new HTMLPurifier_Strategy_MakeWellFormed(); $this->config->set('AutoFormat.AutoParagraph', true); $this->config->set('AutoFormat.Linkify', true); $this->config->set('AutoFormat.RemoveEmpty', true); generate_mock_once('HTMLPurifier_Injector'); }
function test_addFilter_deprecated() { $this->expectError('HTMLPurifier->addFilter() is deprecated, use configuration directives in the Filter namespace or Filter.Custom'); generate_mock_once('HTMLPurifier_Filter'); $this->purifier->addFilter($mock = new HTMLPurifier_FilterMock()); $mock->expectOnce('preFilter'); $mock->expectOnce('postFilter'); $this->purifier->purify('foo'); }
public function setup() { $this->config = HTMLPurifier_Config::create(array('Core.CollectErrors' => true)); $this->context = new HTMLPurifier_Context(); generate_mock_once('HTMLPurifier_ErrorCollector'); $this->collector = new HTMLPurifier_ErrorCollectorEMock(); $this->collector->prepare($this->context); $this->context->register('ErrorCollector', $this->collector); $this->callCount = 0; }
protected function createManager() { $manager = new HTMLPurifier_HTMLModuleManager(); $this->config->set('HTML.CustomDoctype', 'Blank'); $manager->doctypes->register('Blank'); $attrdef_nmtokens = new HTMLPurifier_AttrDef_HTML_Nmtokens(); generate_mock_once('HTMLPurifier_AttrDef'); $attrdef = new HTMLPurifier_AttrDefMock(); $attrdef->setReturnValue('make', $attrdef_nmtokens); $manager->attrTypes->set('NMTOKENS', $attrdef); return $manager; }
public function test_expandIdentifiers() { generate_mock_once('HTMLPurifier_AttrTypes'); $types = new HTMLPurifier_AttrTypesMock(); $collections = new HTMLPurifier_AttrCollections($types, array()); $attr = array('attr1' => 'Color', 'attr2*' => 'URI'); $c_object = new HTMLPurifier_AttrDef_HTML_Color(); $u_object = new HTMLPurifier_AttrDef_URI(); $types->setReturnValue('get', $c_object, array('Color')); $types->setReturnValue('get', $u_object, array('URI')); $collections->expandIdentifiers($attr, $types); $u_object->required = true; $this->assertIdentical($attr, array('attr1' => $c_object, 'attr2' => $u_object)); }
function test() { generate_mock_once('HTMLPurifier_AttrDef'); $config = HTMLPurifier_Config::createDefault(); $context = new HTMLPurifier_Context(); // first test: value properly validates on first definition // so second def is never called $def1 = new HTMLPurifier_AttrDefMock(); $def2 = new HTMLPurifier_AttrDefMock(); $defs = array(&$def1, &$def2); $def = new HTMLPurifier_AttrDef_CSS_Composite_Testable($defs); $input = 'FOOBAR'; $output = 'foobar'; $def1_params = array($input, $config, $context); $def1->expectOnce('validate', $def1_params); $def1->setReturnValue('validate', $output, $def1_params); $def2->expectNever('validate'); $result = $def->validate($input, $config, $context); $this->assertIdentical($output, $result); // second test, first def fails, second def works $def1 = new HTMLPurifier_AttrDefMock(); $def2 = new HTMLPurifier_AttrDefMock(); $defs = array(&$def1, &$def2); $def = new HTMLPurifier_AttrDef_CSS_Composite_Testable($defs); $input = 'BOOMA'; $output = 'booma'; $def_params = array($input, $config, $context); $def1->expectOnce('validate', $def_params); $def1->setReturnValue('validate', false, $def_params); $def2->expectOnce('validate', $def_params); $def2->setReturnValue('validate', $output, $def_params); $result = $def->validate($input, $config, $context); $this->assertIdentical($output, $result); // third test, all fail, so composite faiils $def1 = new HTMLPurifier_AttrDefMock(); $def2 = new HTMLPurifier_AttrDefMock(); $defs = array(&$def1, &$def2); $def = new HTMLPurifier_AttrDef_CSS_Composite_Testable($defs); $input = 'BOOMA'; $output = false; $def_params = array($input, $config, $context); $def1->expectOnce('validate', $def_params); $def1->setReturnValue('validate', false, $def_params); $def2->expectOnce('validate', $def_params); $def2->setReturnValue('validate', false, $def_params); $result = $def->validate($input, $config, $context); $this->assertIdentical($output, $result); }
protected function createFilterMock($expect = true, $result = true, $post = false, $setup = true) { static $i = 0; generate_mock_once('HTMLPurifier_URIFilter'); $mock = new HTMLPurifier_URIFilterMock(); if ($expect) { $mock->expectOnce('filter'); } else { $mock->expectNever('filter'); } $mock->setReturnValue('filter', $result); $mock->setReturnValue('prepare', $setup); $mock->name = $i++; $mock->post = $post; return $mock; }
public function testAttributesTransformedGlobalPre() { $def = $this->config->getHTMLDefinition(true); generate_mock_once('HTMLPurifier_AttrTransform'); $transform = new HTMLPurifier_AttrTransformMock(); $input = array('original' => 'value'); $output = array('class' => 'value'); // must be valid $transform->setReturnValue('transform', $output, array($input, new AnythingExpectation(), new AnythingExpectation())); $def->info_attr_transform_pre[] = $transform; $token = new HTMLPurifier_Token_Start('span', $input, 1); $this->invoke($token); $result = $this->collector->getRaw(); $expect = array(array(1, E_NOTICE, 'Attributes on <span> transformed from original to class', array())); $this->assertIdentical($result, $expect); }
public function testStandardUsage() { generate_mock_once('HTMLPurifier_IDAccumulator'); $this->assertFalse($this->context->exists('IDAccumulator')); $accumulator = new HTMLPurifier_IDAccumulatorMock(); $this->context->register('IDAccumulator', $accumulator); $this->assertTrue($this->context->exists('IDAccumulator')); $accumulator_2 =& $this->context->get('IDAccumulator'); $this->assertReference($accumulator, $accumulator_2); $this->context->destroy('IDAccumulator'); $this->assertFalse($this->context->exists('IDAccumulator')); $this->expectError('Attempted to retrieve non-existent variable IDAccumulator'); $accumulator_3 =& $this->context->get('IDAccumulator'); $this->assertNull($accumulator_3); $this->expectError('Attempted to destroy non-existent variable IDAccumulator'); $this->context->destroy('IDAccumulator'); }
public function test_parseContents() { $module = new HTMLPurifier_HTMLModule(); // pre-defined templates $this->assertIdentical($module->parseContents('Inline'), array('optional', 'Inline | #PCDATA')); $this->assertIdentical($module->parseContents('Flow'), array('optional', 'Flow | #PCDATA')); $this->assertIdentical($module->parseContents('Empty'), array('empty', '')); // normalization procedures $this->assertIdentical($module->parseContents('optional: a'), array('optional', 'a')); $this->assertIdentical($module->parseContents('OPTIONAL :a'), array('optional', 'a')); $this->assertIdentical($module->parseContents('Optional: a'), array('optional', 'a')); // others $this->assertIdentical($module->parseContents('Optional: a | b | c'), array('optional', 'a | b | c')); // object pass-through generate_mock_once('HTMLPurifier_AttrDef'); $this->assertIdentical($module->parseContents(new HTMLPurifier_AttrDefMock()), array(null, null)); }
public function setup() { generate_mock_once('HTMLPurifier_Language'); generate_mock_once('HTMLPurifier_Generator'); parent::setup(); $this->language = new HTMLPurifier_LanguageMock(); $this->language->setReturnValue('getErrorName', 'Error', array(E_ERROR)); $this->language->setReturnValue('getErrorName', 'Warning', array(E_WARNING)); $this->language->setReturnValue('getErrorName', 'Notice', array(E_NOTICE)); // this might prove to be troublesome if we need to set config $this->generator = new HTMLPurifier_Generator($this->config, $this->context); $this->line = false; $this->context->register('Locale', $this->language); $this->context->register('CurrentLine', $this->line); $this->context->register('Generator', $this->generator); $this->collector = new HTMLPurifier_ErrorCollector($this->context); }
function test_isOld() { // using null subclass because parent is abstract $cache = new HTMLPurifier_DefinitionCache_Null('Test'); generate_mock_once('HTMLPurifier_Config'); $config = new HTMLPurifier_ConfigMock(); $config->version = '1.0.0'; // hopefully no conflicts $config->setReturnValue('get', 10, array('Test', 'DefinitionRev')); $config->setReturnValue('getBatchSerial', 'hash', array('Test')); $this->assertIdentical($cache->isOld('1.0.0,hash,10', $config), false); $this->assertIdentical($cache->isOld('1.5.0,hash,1', $config), true); $this->assertIdentical($cache->isOld('0.9.0,hash,1', $config), true); $this->assertIdentical($cache->isOld('1.0.0,hash,1', $config), true); $this->assertIdentical($cache->isOld('1.0.0beta,hash,11', $config), true); $this->assertIdentical($cache->isOld('0.9.0,hash2,1', $config), true); $this->assertIdentical($cache->isOld('1.0.0,hash2,1', $config), false); // if hash is different, don't touch! $this->assertIdentical($cache->isOld('1.0.0beta,hash2,11', $config), true); $this->assertIdentical($cache->isOld('1.0.0-dev,hash2,11', $config), true); }
function test() { generate_mock_once('HTMLPurifier_DefinitionCache'); $mock = new HTMLPurifier_DefinitionCacheMock(); $mock->type = 'Test'; $cache = new HTMLPurifier_DefinitionCache_Decorator(); $cache = $cache->decorate($mock); $this->assertIdentical($cache->type, $mock->type); $def = $this->generateDefinition(); $config = $this->generateConfigMock(); $mock->expectOnce('add', array($def, $config)); $cache->add($def, $config); $mock->expectOnce('set', array($def, $config)); $cache->set($def, $config); $mock->expectOnce('replace', array($def, $config)); $cache->replace($def, $config); $mock->expectOnce('get', array($config)); $cache->get($config); $mock->expectOnce('flush', array($config)); $cache->flush($config); $mock->expectOnce('cleanup', array($config)); $cache->cleanup($config); }
<?php generate_mock_once('HTMLPurifier_DefinitionCache'); class HTMLPurifier_DefinitionCache_Decorator_CleanupTest extends HTMLPurifier_DefinitionCache_DecoratorHarness { function setup() { $this->cache = new HTMLPurifier_DefinitionCache_Decorator_Cleanup(); parent::setup(); } function setupMockForSuccess($op) { $this->mock->expectOnce($op, array($this->def, $this->config)); $this->mock->setReturnValue($op, true, array($this->def, $this->config)); $this->mock->expectNever('cleanup'); } function setupMockForFailure($op) { $this->mock->expectOnce($op, array($this->def, $this->config)); $this->mock->setReturnValue($op, false, array($this->def, $this->config)); $this->mock->expectOnce('cleanup', array($this->config)); } function test_get() { $this->mock->expectOnce('get', array($this->config)); $this->mock->setReturnValue('get', true, array($this->config)); $this->mock->expectNever('cleanup'); $this->assertEqual($this->cache->get($this->config), $this->def); } function test_get_failure() {
protected function setupCacheMock($type) { // inject our definition cache mock globally (borrowed from // DefinitionFactoryTest) generate_mock_once("HTMLPurifier_DefinitionCacheFactory"); $factory = new HTMLPurifier_DefinitionCacheFactoryMock(); $this->oldFactory = HTMLPurifier_DefinitionCacheFactory::instance(); HTMLPurifier_DefinitionCacheFactory::instance($factory); generate_mock_once("HTMLPurifier_DefinitionCache"); $mock = new HTMLPurifier_DefinitionCacheMock(); $config = HTMLPurifier_Config::createDefault(); $factory->setReturnValue('create', $mock, array($type, $config)); return array($mock, $config); }
<?php generate_mock_once('HTMLPurifier_ErrorCollector'); /** * Extended error collector mock that has the ability to expect context */ class HTMLPurifier_ErrorCollectorEMock extends HTMLPurifier_ErrorCollectorMock { private $_context; private $_expected_context = array(); private $_expected_context_at = array(); public function prepare($context) { $this->_context = $context; } public function expectContext($key, $value) { $this->_expected_context[$key] = $value; } public function expectContextAt($step, $key, $value) { $this->_expected_context_at[$step][$key] = $value; } public function send($v1, $v2) { // test for context $context = SimpleTest::getContext(); $test = $context->getTest(); $mock = $this->mock; foreach ($this->_expected_context as $key => $value) { $test->assertEqual($value, $this->_context->get($key));
function test_injectorMissingNeeded() { generate_mock_once('HTMLPurifier_Injector'); $injector = new HTMLPurifier_InjectorMock(); $injector->name = 'MyInjector'; $injector->setReturnValue('checkNeeded', 'a'); $module = $this->config->getHTMLDefinition(true)->getAnonymousModule(); $module->info_injector[] = $injector; $this->assertIdentical($this->config->getHTMLDefinition()->info_injector, array()); }
function setUp() { generate_mock_once('HTMLPurifier_AttrDef'); $this->mock = new HTMLPurifier_AttrDefMock(); $this->def = new HTMLPurifier_AttrDef_CSS_ImportantDecorator($this->mock, true); }