You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigTest.php 23KB

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