ConfigTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <?php
  2. class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness
  3. {
  4. protected $schema;
  5. protected $oldFactory;
  6. public function setUp() {
  7. // set up a dummy schema object for testing
  8. $this->schema = new HTMLPurifier_ConfigSchema();
  9. }
  10. // test functionality based on ConfigSchema
  11. function testNormal() {
  12. $this->schema->add('Element.Abbr', 'H', 'string', false);
  13. $this->schema->add('Element.Name', 'hydrogen', 'istring', false);
  14. $this->schema->add('Element.Number', 1, 'int', false);
  15. $this->schema->add('Element.Mass', 1.00794, 'float', false);
  16. $this->schema->add('Element.Radioactive', false, 'bool', false);
  17. $this->schema->add('Element.Isotopes', array(1 => true, 2 => true, 3 => true), 'lookup', false);
  18. $this->schema->add('Element.Traits', array('nonmetallic', 'odorless', 'flammable'), 'list', false);
  19. $this->schema->add('Element.IsotopeNames', array(1 => 'protium', 2 => 'deuterium', 3 => 'tritium'), 'hash', false);
  20. $this->schema->add('Element.Object', new stdClass(), 'mixed', false);
  21. $config = new HTMLPurifier_Config($this->schema);
  22. $config->autoFinalize = false;
  23. $config->chatty = false;
  24. // test default value retrieval
  25. $this->assertIdentical($config->get('Element.Abbr'), 'H');
  26. $this->assertIdentical($config->get('Element.Name'), 'hydrogen');
  27. $this->assertIdentical($config->get('Element.Number'), 1);
  28. $this->assertIdentical($config->get('Element.Mass'), 1.00794);
  29. $this->assertIdentical($config->get('Element.Radioactive'), false);
  30. $this->assertIdentical($config->get('Element.Isotopes'), array(1 => true, 2 => true, 3 => true));
  31. $this->assertIdentical($config->get('Element.Traits'), array('nonmetallic', 'odorless', 'flammable'));
  32. $this->assertIdentical($config->get('Element.IsotopeNames'), array(1 => 'protium', 2 => 'deuterium', 3 => 'tritium'));
  33. $this->assertIdentical($config->get('Element.Object'), new stdClass());
  34. // test setting values
  35. $config->set('Element.Abbr', 'Pu');
  36. $config->set('Element.Name', 'PLUTONIUM'); // test decaps
  37. $config->set('Element.Number', '94'); // test parsing
  38. $config->set('Element.Mass', '244.'); // test parsing
  39. $config->set('Element.Radioactive', true);
  40. $config->set('Element.Isotopes', array(238, 239)); // test inversion
  41. $config->set('Element.Traits', 'nuclear, heavy, actinide'); // test parsing
  42. $config->set('Element.IsotopeNames', array(238 => 'Plutonium-238', 239 => 'Plutonium-239'));
  43. $config->set('Element.Object', false); // unmodeled
  44. $this->expectError('Cannot set undefined directive Element.Metal to value');
  45. $config->set('Element.Metal', true);
  46. $this->expectError('Value for Element.Radioactive is of invalid type, should be bool');
  47. $config->set('Element.Radioactive', 'very');
  48. // test value retrieval
  49. $this->assertIdentical($config->get('Element.Abbr'), 'Pu');
  50. $this->assertIdentical($config->get('Element.Name'), 'plutonium');
  51. $this->assertIdentical($config->get('Element.Number'), 94);
  52. $this->assertIdentical($config->get('Element.Mass'), 244.);
  53. $this->assertIdentical($config->get('Element.Radioactive'), true);
  54. $this->assertIdentical($config->get('Element.Isotopes'), array(238 => true, 239 => true));
  55. $this->assertIdentical($config->get('Element.Traits'), array('nuclear', 'heavy', 'actinide'));
  56. $this->assertIdentical($config->get('Element.IsotopeNames'), array(238 => 'Plutonium-238', 239 => 'Plutonium-239'));
  57. $this->assertIdentical($config->get('Element.Object'), false);
  58. $this->expectError('Cannot retrieve value of undefined directive Element.Metal');
  59. $config->get('Element.Metal');
  60. }
  61. function testEnumerated() {
  62. // case sensitive
  63. $this->schema->add('Instrument.Manufacturer', 'Yamaha', 'string', false);
  64. $this->schema->addAllowedValues('Instrument.Manufacturer', array(
  65. 'Yamaha' => true, 'Conn-Selmer' => true, 'Vandoren' => true,
  66. 'Laubin' => true, 'Buffet' => true, 'other' => true));
  67. $this->schema->addValueAliases('Instrument.Manufacturer', array(
  68. 'Selmer' => 'Conn-Selmer'));
  69. // case insensitive
  70. $this->schema->add('Instrument.Family', 'woodwind', 'istring', false);
  71. $this->schema->addAllowedValues('Instrument.Family', array(
  72. 'brass' => true, 'woodwind' => true, 'percussion' => true,
  73. 'string' => true, 'keyboard' => true, 'electronic' => true));
  74. $this->schema->addValueAliases('Instrument.Family', array(
  75. 'synth' => 'electronic'));
  76. $config = new HTMLPurifier_Config($this->schema);
  77. $config->autoFinalize = false;
  78. $config->chatty = false;
  79. // case sensitive
  80. $config->set('Instrument.Manufacturer', 'Vandoren');
  81. $this->assertIdentical($config->get('Instrument.Manufacturer'), 'Vandoren');
  82. $config->set('Instrument.Manufacturer', 'Selmer');
  83. $this->assertIdentical($config->get('Instrument.Manufacturer'), 'Conn-Selmer');
  84. $this->expectError('Value not supported, valid values are: Yamaha, Conn-Selmer, Vandoren, Laubin, Buffet, other');
  85. $config->set('Instrument.Manufacturer', 'buffet');
  86. // case insensitive
  87. $config->set('Instrument.Family', 'brass');
  88. $this->assertIdentical($config->get('Instrument.Family'), 'brass');
  89. $config->set('Instrument.Family', 'PERCUSSION');
  90. $this->assertIdentical($config->get('Instrument.Family'), 'percussion');
  91. $config->set('Instrument.Family', 'synth');
  92. $this->assertIdentical($config->get('Instrument.Family'), 'electronic');
  93. $config->set('Instrument.Family', 'Synth');
  94. $this->assertIdentical($config->get('Instrument.Family'), 'electronic');
  95. }
  96. function testNull() {
  97. $this->schema->add('ReportCard.English', null, 'string', true);
  98. $this->schema->add('ReportCard.Absences', 0, 'int', false);
  99. $config = new HTMLPurifier_Config($this->schema);
  100. $config->autoFinalize = false;
  101. $config->chatty = false;
  102. $config->set('ReportCard.English', 'B-');
  103. $this->assertIdentical($config->get('ReportCard.English'), 'B-');
  104. $config->set('ReportCard.English', null); // not yet graded
  105. $this->assertIdentical($config->get('ReportCard.English'), null);
  106. // error
  107. $this->expectError('Value for ReportCard.Absences is of invalid type, should be int');
  108. $config->set('ReportCard.Absences', null);
  109. }
  110. function testAliases() {
  111. $this->schema->add('Home.Rug', 3, 'int', false);
  112. $this->schema->addAlias('Home.Carpet', 'Home.Rug');
  113. $config = new HTMLPurifier_Config($this->schema);
  114. $config->autoFinalize = false;
  115. $config->chatty = false;
  116. $this->assertIdentical($config->get('Home.Rug'), 3);
  117. $this->expectError('Cannot get value from aliased directive, use real name Home.Rug');
  118. $config->get('Home.Carpet');
  119. $this->expectError('Home.Carpet is an alias, preferred directive name is Home.Rug');
  120. $config->set('Home.Carpet', 999);
  121. $this->assertIdentical($config->get('Home.Rug'), 999);
  122. }
  123. // test functionality based on method
  124. function test_getBatch() {
  125. $this->schema->add('Variables.TangentialAcceleration', 'a_tan', 'string', false);
  126. $this->schema->add('Variables.AngularAcceleration', 'alpha', 'string', false);
  127. $config = new HTMLPurifier_Config($this->schema);
  128. $config->autoFinalize = false;
  129. $config->chatty = false;
  130. // grab a namespace
  131. $this->assertIdentical(
  132. $config->getBatch('Variables'),
  133. array(
  134. 'TangentialAcceleration' => 'a_tan',
  135. 'AngularAcceleration' => 'alpha'
  136. )
  137. );
  138. // grab a non-existant namespace
  139. $this->expectError('Cannot retrieve undefined namespace Constants');
  140. $config->getBatch('Constants');
  141. }
  142. function test_loadIni() {
  143. $this->schema->add('Shortcut.Copy', 'c', 'istring', false);
  144. $this->schema->add('Shortcut.Paste', 'v', 'istring', false);
  145. $this->schema->add('Shortcut.Cut', 'x', 'istring', false);
  146. $config = new HTMLPurifier_Config($this->schema);
  147. $config->autoFinalize = false;
  148. $config->loadIni(dirname(__FILE__) . '/ConfigTest-loadIni.ini');
  149. $this->assertIdentical($config->get('Shortcut.Copy'), 'q');
  150. $this->assertIdentical($config->get('Shortcut.Paste'), 'p');
  151. $this->assertIdentical($config->get('Shortcut.Cut'), 't');
  152. }
  153. function test_getHTMLDefinition() {
  154. // we actually want to use the old copy, because the definition
  155. // generation routines have dependencies on configuration values
  156. $config = HTMLPurifier_Config::createDefault();
  157. $config->set('HTML.Doctype', 'XHTML 1.0 Strict');
  158. $config->autoFinalize = false;
  159. $def = $config->getCSSDefinition();
  160. $this->assertIsA($def, 'HTMLPurifier_CSSDefinition');
  161. $def = $config->getHTMLDefinition();
  162. $def2 = $config->getHTMLDefinition();
  163. $this->assertIsA($def, 'HTMLPurifier_HTMLDefinition');
  164. $this->assertTrue($def === $def2);
  165. $this->assertTrue($def->setup);
  166. $old_def = clone $def2;
  167. $config->set('HTML.Doctype', 'HTML 4.01 Transitional');
  168. $def = $config->getHTMLDefinition();
  169. $this->assertIsA($def, 'HTMLPurifier_HTMLDefinition');
  170. $this->assertTrue($def !== $old_def);
  171. $this->assertTrue($def->setup);
  172. }
  173. function test_getHTMLDefinition_deprecatedRawError() {
  174. $config = HTMLPurifier_Config::createDefault();
  175. $config->chatty = false;
  176. // test deprecated retrieval of raw definition
  177. $config->set('HTML.DefinitionID', 'HTMLPurifier_ConfigTest->test_getHTMLDefinition()');
  178. $config->set('HTML.DefinitionRev', 3);
  179. $this->expectError("Useless DefinitionID declaration");
  180. $def = $config->getHTMLDefinition(true);
  181. $this->assertEqual(false, $def->setup);
  182. // auto initialization
  183. $config->getHTMLDefinition();
  184. $this->assertTrue($def->setup);
  185. }
  186. function test_getHTMLDefinition_optimizedRawError() {
  187. $this->expectException(new HTMLPurifier_Exception("Cannot set optimized = true when raw = false"));
  188. $config = HTMLPurifier_Config::createDefault();
  189. $config->getHTMLDefinition(false, true);
  190. }
  191. function test_getHTMLDefinition_rawAfterSetupError() {
  192. $this->expectException(new HTMLPurifier_Exception("Cannot retrieve raw definition after it has already been setup"));
  193. $config = HTMLPurifier_Config::createDefault();
  194. $config->chatty = false;
  195. $config->getHTMLDefinition();
  196. $config->getHTMLDefinition(true);
  197. }
  198. function test_getHTMLDefinition_inconsistentOptimizedError() {
  199. $this->expectError("Useless DefinitionID declaration");
  200. $this->expectException(new HTMLPurifier_Exception("Inconsistent use of optimized and unoptimized raw definition retrievals"));
  201. $config = HTMLPurifier_Config::create(array('HTML.DefinitionID' => 'HTMLPurifier_ConfigTest->test_getHTMLDefinition_inconsistentOptimizedError'));
  202. $config->chatty = false;
  203. $config->getHTMLDefinition(true, false);
  204. $config->getHTMLDefinition(true, true);
  205. }
  206. function test_getHTMLDefinition_inconsistentOptimizedError2() {
  207. $this->expectException(new HTMLPurifier_Exception("Inconsistent use of optimized and unoptimized raw definition retrievals"));
  208. $config = HTMLPurifier_Config::create(array('HTML.DefinitionID' => 'HTMLPurifier_ConfigTest->test_getHTMLDefinition_inconsistentOptimizedError2'));
  209. $config->chatty = false;
  210. $config->getHTMLDefinition(true, true);
  211. $config->getHTMLDefinition(true, false);
  212. }
  213. function test_getHTMLDefinition_rawError() {
  214. $config = HTMLPurifier_Config::createDefault();
  215. $this->expectException(new HTMLPurifier_Exception('Cannot retrieve raw version without specifying %HTML.DefinitionID'));
  216. $def = $config->getHTMLDefinition(true, true);
  217. }
  218. function test_getCSSDefinition() {
  219. $config = HTMLPurifier_Config::createDefault();
  220. $def = $config->getCSSDefinition();
  221. $this->assertIsA($def, 'HTMLPurifier_CSSDefinition');
  222. }
  223. function test_getDefinition() {
  224. $this->schema->add('Cache.DefinitionImpl', null, 'string', true);
  225. $config = new HTMLPurifier_Config($this->schema);
  226. $this->expectException(new HTMLPurifier_Exception("Definition of Crust type not supported"));
  227. $config->getDefinition('Crust');
  228. }
  229. function test_loadArray() {
  230. // setup a few dummy namespaces/directives for our testing
  231. $this->schema->add('Zoo.Aadvark', 0, 'int', false);
  232. $this->schema->add('Zoo.Boar', 0, 'int', false);
  233. $this->schema->add('Zoo.Camel', 0, 'int', false);
  234. $this->schema->add('Zoo.Others', array(), 'list', false);
  235. $config_manual = new HTMLPurifier_Config($this->schema);
  236. $config_loadabbr = new HTMLPurifier_Config($this->schema);
  237. $config_loadfull = new HTMLPurifier_Config($this->schema);
  238. $config_manual->set('Zoo.Aadvark', 3);
  239. $config_manual->set('Zoo.Boar', 5);
  240. $config_manual->set('Zoo.Camel', 2000); // that's a lotta camels!
  241. $config_manual->set('Zoo.Others', array('Peacock', 'Dodo')); // wtf!
  242. // condensed form
  243. $config_loadabbr->loadArray(array(
  244. 'Zoo.Aadvark' => 3,
  245. 'Zoo.Boar' => 5,
  246. 'Zoo.Camel' => 2000,
  247. 'Zoo.Others' => array('Peacock', 'Dodo')
  248. ));
  249. // fully expanded form
  250. $config_loadfull->loadArray(array(
  251. 'Zoo' => array(
  252. 'Aadvark' => 3,
  253. 'Boar' => 5,
  254. 'Camel' => 2000,
  255. 'Others' => array('Peacock', 'Dodo')
  256. )
  257. ));
  258. $this->assertIdentical($config_manual, $config_loadabbr);
  259. $this->assertIdentical($config_manual, $config_loadfull);
  260. }
  261. function test_create() {
  262. $this->schema->add('Cake.Sprinkles', 666, 'int', false);
  263. $this->schema->add('Cake.Flavor', 'vanilla', 'string', false);
  264. $config = new HTMLPurifier_Config($this->schema);
  265. $config->set('Cake.Sprinkles', 42);
  266. // test flat pass-through
  267. $created_config = HTMLPurifier_Config::create($config, $this->schema);
  268. $this->assertIdentical($config, $created_config);
  269. // test loadArray
  270. $created_config = HTMLPurifier_Config::create(array('Cake.Sprinkles' => 42), $this->schema);
  271. $this->assertIdentical($config, $created_config);
  272. // test loadIni
  273. $created_config = HTMLPurifier_Config::create(dirname(__FILE__) . '/ConfigTest-create.ini', $this->schema);
  274. $this->assertIdentical($config, $created_config);
  275. }
  276. function test_finalize() {
  277. // test finalization
  278. $this->schema->add('Poem.Meter', 'iambic', 'string', false);
  279. $config = new HTMLPurifier_Config($this->schema);
  280. $config->autoFinalize = false;
  281. $config->chatty = false;
  282. $config->set('Poem.Meter', 'irregular');
  283. $config->finalize();
  284. $this->expectError('Cannot set directive after finalization');
  285. $config->set('Poem.Meter', 'vedic');
  286. $this->expectError('Cannot load directives after finalization');
  287. $config->loadArray(array('Poem.Meter' => 'octosyllable'));
  288. $this->expectError('Cannot load directives after finalization');
  289. $config->loadIni(dirname(__FILE__) . '/ConfigTest-finalize.ini');
  290. }
  291. function test_loadArrayFromForm() {
  292. $this->schema->add('Pancake.Mix', 'buttermilk', 'string', false);
  293. $this->schema->add('Pancake.Served', true, 'bool', false);
  294. $this->schema->add('Toppings.Syrup', true, 'bool', false);
  295. $this->schema->add('Toppings.Flavor', 'maple', 'string', false);
  296. $this->schema->add('Toppings.Strawberries', 3, 'int', false);
  297. $this->schema->add('Toppings.Calories', 2000, 'int', true);
  298. $this->schema->add('Toppings.DefinitionID', null, 'string', true);
  299. $this->schema->add('Toppings.DefinitionRev', 1, 'int', false);
  300. $this->schema->add('Toppings.Protected', 1, 'int', false);
  301. $get = array(
  302. 'breakfast' => array(
  303. 'Pancake.Mix' => 'nasty',
  304. 'Pancake.Served' => '0',
  305. 'Toppings.Syrup' => '0',
  306. 'Toppings.Flavor' => "juice",
  307. 'Toppings.Strawberries' => '999',
  308. 'Toppings.Calories' => '',
  309. 'Null_Toppings.Calories' => '1',
  310. 'Toppings.DefinitionID' => '<argh>',
  311. 'Toppings.DefinitionRev' => '65',
  312. 'Toppings.Protected' => '4',
  313. )
  314. );
  315. $config_expect = HTMLPurifier_Config::create(array(
  316. 'Pancake.Served' => false,
  317. 'Toppings.Syrup' => false,
  318. 'Toppings.Flavor' => "juice",
  319. 'Toppings.Strawberries' => 999,
  320. 'Toppings.Calories' => null
  321. ), $this->schema);
  322. $config_result = HTMLPurifier_Config::loadArrayFromForm(
  323. $get, 'breakfast',
  324. array('Pancake.Served', 'Toppings', '-Toppings.Protected'),
  325. false, // mq fix
  326. $this->schema
  327. );
  328. $this->assertEqual($config_expect, $config_result);
  329. /*
  330. MAGIC QUOTES NOT TESTED!!!
  331. $get = array(
  332. 'breakfast' => array(
  333. 'Pancake.Mix' => 'n\\asty'
  334. )
  335. );
  336. $config_expect = HTMLPurifier_Config::create(array(
  337. 'Pancake.Mix' => 'n\\asty'
  338. ));
  339. $config_result = HTMLPurifier_Config::loadArrayFromForm($get, 'breakfast', true, false);
  340. $this->assertEqual($config_expect, $config_result);
  341. */
  342. }
  343. function test_getAllowedDirectivesForForm() {
  344. $this->schema->add('Unused.Unused', 'Foobar', 'string', false);
  345. $this->schema->add('Partial.Allowed', true, 'bool', false);
  346. $this->schema->add('Partial.Unused', 'Foobar', 'string', false);
  347. $this->schema->add('All.Allowed', true, 'bool', false);
  348. $this->schema->add('All.Blacklisted', 'Foobar', 'string', false); // explicitly blacklisted
  349. $this->schema->add('All.DefinitionID', 'Foobar', 'string', true); // auto-blacklisted
  350. $this->schema->add('All.DefinitionRev', 2, 'int', false); // auto-blacklisted
  351. $input = array('Partial.Allowed', 'All', '-All.Blacklisted');
  352. $output = HTMLPurifier_Config::getAllowedDirectivesForForm($input, $this->schema);
  353. $expect = array(
  354. array('Partial', 'Allowed'),
  355. array('All', 'Allowed')
  356. );
  357. $this->assertEqual($output, $expect);
  358. }
  359. function testDeprecatedAPI() {
  360. $this->schema->add('Foo.Bar', 2, 'int', false);
  361. $config = new HTMLPurifier_Config($this->schema);
  362. $config->chatty = false;
  363. $this->expectError('Using deprecated API: use $config->set(\'Foo.Bar\', ...) instead');
  364. $config->set('Foo', 'Bar', 4);
  365. $this->expectError('Using deprecated API: use $config->get(\'Foo.Bar\') instead');
  366. $this->assertIdentical($config->get('Foo', 'Bar'), 4);
  367. }
  368. function testInherit() {
  369. $this->schema->add('Phantom.Masked', 25, 'int', false);
  370. $this->schema->add('Phantom.Unmasked', 89, 'int', false);
  371. $this->schema->add('Phantom.Latemasked', 11, 'int', false);
  372. $config = new HTMLPurifier_Config($this->schema);
  373. $config->set('Phantom.Masked', 800);
  374. $subconfig = HTMLPurifier_Config::inherit($config);
  375. $config->set('Phantom.Latemasked', 100, 'int', false);
  376. $this->assertIdentical($subconfig->get('Phantom.Masked'), 800);
  377. $this->assertIdentical($subconfig->get('Phantom.Unmasked'), 89);
  378. $this->assertIdentical($subconfig->get('Phantom.Latemasked'), 100);
  379. }
  380. function testSerialize() {
  381. $config = HTMLPurifier_Config::createDefault();
  382. $config->set('HTML.Allowed', 'a');
  383. $config2 = unserialize($config->serialize());
  384. $this->assertIdentical($config->get('HTML.Allowed'), $config2->get('HTML.Allowed'));
  385. }
  386. function testDefinitionCachingNothing() {
  387. list($mock, $config) = $this->setupCacheMock('HTML');
  388. // should not touch the cache
  389. $mock->expectNever('get');
  390. $mock->expectNever('add');
  391. $mock->expectNever('set');
  392. $config->getDefinition('HTML', true);
  393. $config->getDefinition('HTML', true);
  394. $config->getDefinition('HTML');
  395. $this->teardownCacheMock();
  396. }
  397. function testDefinitionCachingOptimized() {
  398. list($mock, $config) = $this->setupCacheMock('HTML');
  399. $mock->expectNever('set');
  400. $config->set('HTML.DefinitionID', 'HTMLPurifier_ConfigTest->testDefinitionCachingOptimized');
  401. $mock->expectOnce('get');
  402. $mock->setReturnValue('get', null);
  403. $this->assertTrue($config->maybeGetRawHTMLDefinition());
  404. $this->assertTrue($config->maybeGetRawHTMLDefinition());
  405. $mock->expectOnce('add');
  406. $config->getDefinition('HTML');
  407. $this->teardownCacheMock();
  408. }
  409. function testDefinitionCachingOptimizedHit() {
  410. $fake_config = HTMLPurifier_Config::createDefault();
  411. $fake_def = $fake_config->getHTMLDefinition();
  412. list($mock, $config) = $this->setupCacheMock('HTML');
  413. // should never frob cache
  414. $mock->expectNever('add');
  415. $mock->expectNever('set');
  416. $config->set('HTML.DefinitionID', 'HTMLPurifier_ConfigTest->testDefinitionCachingOptimizedHit');
  417. $mock->expectOnce('get');
  418. $mock->setReturnValue('get', $fake_def);
  419. $this->assertNull($config->maybeGetRawHTMLDefinition());
  420. $config->getDefinition('HTML');
  421. $config->getDefinition('HTML');
  422. $this->teardownCacheMock();
  423. }
  424. protected function setupCacheMock($type) {
  425. // inject our definition cache mock globally (borrowed from
  426. // DefinitionFactoryTest)
  427. generate_mock_once("HTMLPurifier_DefinitionCacheFactory");
  428. $factory = new HTMLPurifier_DefinitionCacheFactoryMock();
  429. $this->oldFactory = HTMLPurifier_DefinitionCacheFactory::instance();
  430. HTMLPurifier_DefinitionCacheFactory::instance($factory);
  431. generate_mock_once("HTMLPurifier_DefinitionCache");
  432. $mock = new HTMLPurifier_DefinitionCacheMock();
  433. $config = HTMLPurifier_Config::createDefault();
  434. $factory->setReturnValue('create', $mock, array($type, $config));
  435. return array($mock, $config);
  436. }
  437. protected function teardownCacheMock() {
  438. HTMLPurifier_DefinitionCacheFactory::instance($this->oldFactory);
  439. }
  440. }
  441. // vim: et sw=4 sts=4