barre_outil_markitup.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /*
  3. * Plugin Porte Plume pour SPIP 2
  4. * Licence GPL
  5. * Auteur Matthieu Marcillaud
  6. */
  7. require_once('lanceur_spip.php');
  8. class Test_barre_outil_markitup extends SpipTest{
  9. var $baseParamsBarre = array();
  10. var $baseParamsBarreEtendue = array();
  11. function __construct() {
  12. parent::__construct("Test de la classe Barre_outils");
  13. // instancier une barre d'outil
  14. include_spip('porte_plume_fonctions');
  15. $this->baseParamsBarre = array(
  16. 'nameSpace' => 'spip',
  17. 'markupSet' => array(
  18. // H1 - {{{
  19. array(
  20. "id" => 'header1',
  21. "name" => _T('barreoutils:barre_intertitre'),
  22. "key" => "H",
  23. "className" => "outil_header1",
  24. "openWith" => "{{{",
  25. "closeWith" => "}}}",
  26. "display" => true,
  27. )));
  28. $p = $this->baseParamsBarre;
  29. $p['markupSet'][1] = array(
  30. "id" => 'couleurs',
  31. "name" => _T('barreoutils:barre_couleur'),
  32. "key" => "C",
  33. "className" => "outil_couleur",
  34. "openWith" => '[color=[![Color]!]]',
  35. "closeWith" => '[/color]',
  36. "display" => true,
  37. "dropMenu" => array(
  38. array(
  39. "id" => "couleur_jaune",
  40. "name" => 'Yellow',
  41. "openWith" => '[color=yellow]',
  42. "closeWith" => '[/color]',
  43. "className" => "outil_couleur",
  44. "display" => true,
  45. ),
  46. array(
  47. "id" => "couleur_orange",
  48. "name" => 'Orange',
  49. "openWith" => '[color=orange]',
  50. "closeWith" => '[/color]',
  51. "className" => "outil_couleur",
  52. "display" => true,
  53. ),
  54. array(
  55. "id" => "couleur_rouge",
  56. "name" => 'Red',
  57. "openWith" => '[color=red]',
  58. "closeWith" => '[/color]',
  59. "className" => "outil_couleur",
  60. "display" => true,
  61. ),
  62. ),
  63. );
  64. $this->baseParamsBarreEtendue = $p;
  65. }
  66. // avant chaque appel de fonction test
  67. function setUp() {
  68. }
  69. // apres chaque appel de fonction test
  70. function tearDown() {
  71. }
  72. function testInitialisationBarre(){
  73. // parametres inseres a leur bonne place
  74. $b = new Barre_outils($this->baseParamsBarre);
  75. $this->assertEqual('spip', $b->nameSpace);
  76. $this->assertEqual('header1', $b->markupSet[0]['id']);
  77. $this->assertEqual(7, count($b->markupSet[0]));
  78. }
  79. function testInitialisationBarreEtendue(){
  80. // parametres inseres a leur bonne place,
  81. // meme quand il y a des sous-menu d'icones
  82. $b = new Barre_outils($this->baseParamsBarreEtendue);
  83. $this->assertEqual('spip', $b->nameSpace);
  84. $this->assertEqual('header1', $b->markupSet[0]['id']);
  85. $this->assertEqual(7, count($b->markupSet[0]));
  86. $this->assertEqual('couleurs', $b->markupSet[1]['id']);
  87. $this->assertEqual(3, count($b->markupSet[1]['dropMenu']));
  88. }
  89. function testOptionsIncorrectesNonIncluses(){
  90. $p = $this->baseParamsBarre;
  91. $p['fausseVariable'] = "je ne dois pas m'installer";
  92. $p['markupSet'][0]['fauxParam'] = "je ne dois pas m'installer";
  93. $b = new Barre_outils($p);
  94. $this->assertEqual('spip', $b->nameSpace);
  95. $this->expectError(new PatternExpectation("/Undefined property: Barre_outils::\\\$fausseVariable/i"));
  96. $b->fausseVariable;
  97. $this->expectError(new PatternExpectation("/Undefined index: fauxParam/i"));
  98. $b->markupSet[0]['fauxParam'];
  99. $this->assertEqual(7, count($b->markupSet[0]));
  100. }
  101. function testRecuperationDeParametreAvecGet(){
  102. // trouver des id de premier niveau
  103. $p = $this->baseParamsBarre;
  104. $b = new Barre_outils($p);
  105. $this->assertEqual($b->get('header1'), $p['markupSet'][0]);
  106. // trouver des id de second niveau
  107. $p = $this->baseParamsBarreEtendue;
  108. $b = new Barre_outils($p);
  109. $this->assertEqual($b->get('header1'), $p['markupSet'][0]);
  110. $this->assertEqual($b->get('couleurs'), $p['markupSet'][1]);
  111. $this->assertEqual($b->get('couleur_jaune'), $p['markupSet'][1]['dropMenu'][0]);
  112. $this->assertEqual($b->get('couleur_orange'), $p['markupSet'][1]['dropMenu'][1]);
  113. $this->assertEqual($b->get('couleur_rouge'), $p['markupSet'][1]['dropMenu'][2]);
  114. // ne pas trouver d'id inconnu
  115. $this->assertFalse($b->get('je_nexiste_pas'));
  116. }
  117. function testModificationDeParametresAvecSet(){
  118. $p = $this->baseParamsBarre;
  119. $b = new Barre_outils($p);
  120. $p['markupSet'][0]['name'] = 'New';
  121. $r = $p['markupSet'][0];
  122. $x = $b->set('header1', array("name"=>"New"));
  123. $this->assertEqual($r, $x); // set retourne la chaine modifiee complete
  124. $this->assertEqual($r, $b->get('header1'));
  125. // on ne peut ajouter de mauvais parametres
  126. $x = $b->set('header1', array("Je Suis Pas Bon"=>"No no no"));
  127. $this->assertEqual($r, $x); // set retourne la chaine modifiee complete
  128. $this->assertEqual($r, $b->get('header1'));
  129. }
  130. function testAjoutDeParametresApres(){
  131. $b = new Barre_outils($this->baseParamsBarre);
  132. $p = $this->baseParamsBarreEtendue;
  133. // ajoutons la couleur apres
  134. $b->ajouterApres('header1',$p['markupSet'][1]);
  135. $this->assertEqual(2, count($b->markupSet)); // 2 boutons de premier niveau maintenant
  136. $this->assertEqual($b->get('couleurs'), $p['markupSet'][1]); // get renvoie bien le bon ajout
  137. $this->assertEqual($b->markupSet[1], $p['markupSet'][1]); // et l'ajout est au bon endroit
  138. // ajoutons une couleur dans l'ajout
  139. $coul = $p['markupSet'][1]['dropMenu'][0];
  140. $coul['id'] = 'couleur_violette';
  141. $b->ajouterApres('couleur_orange',$coul);
  142. $this->assertEqual(4, count($b->markupSet[1]['dropMenu'])); // sous boutons
  143. $this->assertEqual($b->get('couleur_violette'), $coul);
  144. $this->assertEqual($b->markupSet[1]['dropMenu'][2], $coul); // insertion au bon endroit
  145. // ajoutons un header2 encore apres
  146. $p['markupSet'][0]['id'] = 'header2';
  147. $b->ajouterApres('couleurs',$p['markupSet'][0]);
  148. $this->assertEqual(3, count($b->markupSet));
  149. $this->assertEqual($b->get('header2'), $p['markupSet'][0]);
  150. $this->assertEqual($b->markupSet[2], $p['markupSet'][0]);
  151. }
  152. function testAjoutDeParametresAvant(){
  153. $b = new Barre_outils($this->baseParamsBarre);
  154. $p = $this->baseParamsBarreEtendue;
  155. // ajoutons la couleur apres
  156. $b->ajouterAvant('header1',$p['markupSet'][1]);
  157. $this->assertEqual(2, count($b->markupSet)); // 2 boutons de premier niveau maintenant
  158. $this->assertEqual($b->get('couleurs'), $p['markupSet'][1]); // get renvoie bien le bon ajout
  159. $this->assertEqual($b->markupSet[0], $p['markupSet'][1]); // et l'ajout est au bon endroit
  160. // ajoutons une couleur dans l'ajout
  161. $coul = $p['markupSet'][1]['dropMenu'][0];
  162. $coul['id'] = 'couleur_violette';
  163. $b->ajouterAvant('couleur_orange',$coul);
  164. $this->assertEqual(4, count($b->markupSet[0]['dropMenu'])); // sous boutons
  165. $this->assertEqual($b->get('couleur_violette'), $coul);
  166. $this->assertEqual($b->markupSet[0]['dropMenu'][1], $coul); // insertion au bon endroit
  167. // ajoutons un header2 avant le 1
  168. $p['markupSet'][0]['id'] = 'header2';
  169. $b->ajouterAvant('header1',$p['markupSet'][0]);
  170. $this->assertEqual(3, count($b->markupSet));
  171. $this->assertEqual($b->get('header2'), $p['markupSet'][0]);
  172. $this->assertEqual($b->markupSet[1], $p['markupSet'][0]);
  173. }
  174. function testAfficherEtCacher(){
  175. $b = new Barre_outils($this->baseParamsBarre);
  176. $b->cacher('header1');
  177. $this->assertFalse($b->markupSet[0]['display']);
  178. $b->afficher('header1');
  179. $this->assertTrue($b->markupSet[0]['display']);
  180. }
  181. function testAfficherEtCacherTout(){
  182. $b = new Barre_outils($this->baseParamsBarreEtendue);
  183. $b->cacherTout();
  184. $this->assertFalse($b->markupSet[0]['display']);
  185. $this->assertFalse($b->markupSet[1]['dropMenu'][0]['display']);
  186. $b->afficherTout();
  187. $this->assertTrue($b->markupSet[0]['display']);
  188. $this->assertTrue($b->markupSet[1]['dropMenu'][0]['display']);
  189. }
  190. function testAfficherEtCacherPlusieursBoutons(){
  191. $b = new Barre_outils($this->baseParamsBarreEtendue);
  192. $b->cacher(array('header1','couleur_jaune'));
  193. $this->assertFalse($b->markupSet[0]['display']);
  194. $this->assertFalse($b->markupSet[1]['dropMenu'][0]['display']);
  195. $this->assertTrue($b->markupSet[1]['dropMenu'][1]['display']);
  196. $b->cacherTout();
  197. $b->afficher(array('header1','couleur_jaune'));
  198. $this->assertTrue($b->markupSet[0]['display']);
  199. $this->assertTrue($b->markupSet[1]['dropMenu'][0]['display']);
  200. $this->assertFalse($b->markupSet[1]['dropMenu'][1]['display']);
  201. }
  202. function testSetAvecIdVideNeDoitRienModifier(){
  203. $b = new Barre_outils($this->baseParamsBarreEtendue);
  204. $b->set(array(),array('display'=>false));
  205. $this->assertTrue($b->markupSet[0]['display']);
  206. $this->assertTrue($b->markupSet[1]['dropMenu'][0]['display']);
  207. }
  208. function testSetAvecIdArrayDoitModifTousLesIds(){
  209. $b = new Barre_outils($this->baseParamsBarreEtendue);
  210. $b->set(array('header1','couleur_jaune'),array('display'=>false));
  211. $this->assertFalse($b->markupSet[0]['display']);
  212. $this->assertFalse($b->markupSet[1]['dropMenu'][0]['display']);
  213. $this->assertTrue($b->markupSet[1]['dropMenu'][1]['display']);
  214. }
  215. function testCreerJson(){
  216. $b = new Barre_outils($this->baseParamsBarre);
  217. $b->ajouterApres('header1', array(
  218. "id" => 'Caracteres decodes',
  219. "name" => "&eacute;trange",
  220. "className" => "outil_fr",
  221. "openWith" => "[fr]",
  222. "display" => true,
  223. ));
  224. $json = $b->creer_json();
  225. $this->assertPattern(',barre_outils_spip = {,',$json);
  226. $this->assertPattern(',\[{"name":",',$json);
  227. $this->assertNoPattern(',eacute;,',$json);
  228. }
  229. function testBoutonsDUneLangue(){
  230. $b = new Barre_outils($this->baseParamsBarre);
  231. $ico2 = $ico1 = array(
  232. "id" => 'ico_fr1',
  233. "name" => "test apparaissant si langue est le francais",
  234. "className" => "outil_fr",
  235. "openWith" => "[fr]",
  236. "lang" => array("fr"),
  237. "display" => true,
  238. );
  239. $ico2['id'] = 'ico_fr2';
  240. $ico2['lang'] = array("fr","en","es");
  241. $b->ajouterApres('header1', $ico1);
  242. $b->ajouterApres('ico_fr1', $ico2);
  243. $this->assertEqual($ico1, $b->get('ico_fr1'));
  244. $this->assertEqual($ico2, $b->get('ico_fr2'));
  245. // verifier que ces nouveaux array()
  246. // ne posent pas de problemes dans les recursions
  247. $b->cacherTout();
  248. $this->assertFalse($b->markupSet[1]['display']);
  249. $b->afficher('ico_fr1');
  250. $this->assertTrue($b->markupSet[1]['display']);
  251. $b->cacherTout();
  252. $b->afficher(array('ico_fr1','ico_fr2'));
  253. $this->assertTrue($b->markupSet[1]['display']);
  254. // la langue est bien transmise au json
  255. $json = $b->creer_json();
  256. $this->assertPattern(',"lang":\[,', $json);
  257. }
  258. function testFonctionsJavacriptDansParametreNeDoitPasEtreEntreguillemetsDansJson(){
  259. $b = new Barre_outils($this->baseParamsBarre);
  260. $clean = array(
  261. "id" => 'clean',
  262. "name" => _T('barreoutils:barre_clean'),
  263. "className" => "outil_clean",
  264. // function doit etre echappe
  265. "replaceWith" => 'function(markitup) { return markitup.selection.replace(/<(.*?)>/g, "") }',
  266. "display" => true,
  267. );
  268. $b->ajouterApres('header1', $clean);
  269. $json = $b->creer_json();
  270. // pas de :"function(... ..."
  271. $this->assertPattern('/:function\(/',$json);
  272. }
  273. function testParametreFunctionsDansJson(){
  274. $b = new Barre_outils($this->baseParamsBarre);
  275. $b->functions = "function dido(){}";
  276. $json = $b->creer_json();
  277. // function n'est plus dans la barre
  278. $this->expectError(new PatternExpectation("/Undefined property: Barre_outils::\\\$functions/i"));
  279. $b->functions;
  280. // mais uniquement en fin du fichier json
  281. $this->assertPattern('/function dido\(/', $json);
  282. }
  283. function testAjouterFonctions(){
  284. $b = new Barre_outils($this->baseParamsBarre);
  285. $b->ajouterFonction("function dido(){}");
  286. $this->assertPattern('/function dido\(/', $b->functions);
  287. }
  288. /*
  289. function squeletteTest(){
  290. $sq = new SqueletteTest("SimpleTest");
  291. $sq->addInsertHead();
  292. $sq->addToBody("
  293. <div class='formulaire_spip'>
  294. <form>
  295. <textarea name='texte' class='texte'></textarea>
  296. </form>
  297. </div>
  298. ");
  299. return $sq;
  300. }
  301. function testPresenceBarreOutilPublique(){
  302. include_spip('simpletest/browser');
  303. include_spip('simpletest/web_tester');
  304. $sq = $this->squeletteTest();
  305. $browser = &new SimpleBrowser();
  306. $browser->get($f=$this->urlTestCode($sq->code()));
  307. $browser->setField('texte', 'me');
  308. $this->dump($browser->getField('texte'));
  309. $this->dump($browser->getContent());
  310. #$this->dump($c);
  311. #$this->assertPattern('/jquery\.markitup_pour_spip\.js/', $c);
  312. }*/
  313. }
  314. ?>