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