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