3 * Plugin Porte Plume pour SPIP 2
5 * Auteur Matthieu Marcillaud
7 require_once('lanceur_spip.php');
9 class Test_barre_outil_markitup
extends SpipTest
{
11 var $baseParamsBarre = array();
12 var $baseParamsBarreEtendue = array();
14 function __construct() {
16 parent
::__construct("Test de la classe Barre_outils");
18 // instancier une barre d'outil
19 include_spip('porte_plume_fonctions');
21 $this->baseParamsBarre
= array(
22 'nameSpace' => 'spip',
27 "name" => _T('barreoutils:barre_intertitre'),
29 "className" => "outil_header1",
34 $p = $this->baseParamsBarre
;
35 $p['markupSet'][1] = array(
37 "name" => _T('barreoutils:barre_couleur'),
39 "className" => "outil_couleur",
40 "openWith" => '[color=[![Color]!]]',
41 "closeWith" => '[/color]',
45 "id" => "couleur_jaune",
47 "openWith" => '[color=yellow]',
48 "closeWith" => '[/color]',
49 "className" => "outil_couleur",
53 "id" => "couleur_orange",
55 "openWith" => '[color=orange]',
56 "closeWith" => '[/color]',
57 "className" => "outil_couleur",
61 "id" => "couleur_rouge",
63 "openWith" => '[color=red]',
64 "closeWith" => '[/color]',
65 "className" => "outil_couleur",
70 $this->baseParamsBarreEtendue
= $p;
73 // avant chaque appel de fonction test
78 // apres chaque appel de fonction test
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]));
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']));
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);
110 $this->expectError(new PatternExpectation("/Undefined property: Barre_outils::\\\$fausseVariable/i"));
113 $this->expectError(new PatternExpectation("/Undefined index: fauxParam/i"));
114 $b->markupSet
[0]['fauxParam'];
116 $this->assertEqual(7, count($b->markupSet
[0]));
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]);
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]);
134 // ne pas trouver d'id inconnu
135 $this->assertFalse($b->get('je_nexiste_pas'));
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"));
145 $this->assertEqual($r, $x); // set retourne la chaine modifiee complete
146 $this->assertEqual($r, $b->get('header1'));
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'));
154 function testAjoutDeParametresApres(){
155 $b = new Barre_outils($this->baseParamsBarre
);
156 $p = $this->baseParamsBarreEtendue
;
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
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
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]);
180 function testAjoutDeParametresAvant(){
181 $b = new Barre_outils($this->baseParamsBarre
);
182 $p = $this->baseParamsBarreEtendue
;
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
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
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]);
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']);
214 function testAfficherEtCacherTout(){
215 $b = new Barre_outils($this->baseParamsBarreEtendue
);
217 $this->assertFalse($b->markupSet
[0]['display']);
218 $this->assertFalse($b->markupSet
[1]['dropMenu'][0]['display']);
221 $this->assertTrue($b->markupSet
[0]['display']);
222 $this->assertTrue($b->markupSet
[1]['dropMenu'][0]['display']);
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']);
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']);
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']);
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']);
254 function testCreerJson(){
255 $b = new Barre_outils($this->baseParamsBarre
);
256 $b->ajouterApres('header1', array(
257 "id" => 'Caracteres decodes',
258 "name" => "étrange",
259 "className" => "outil_fr",
260 "openWith" => "[fr]",
263 $json = $b->creer_json();
264 $this->assertPattern(',barre_outils_spip = {,',$json);
265 $this->assertPattern(',\[{"name":",',$json);
266 $this->assertNoPattern(',eacute;,',$json);
269 function testBoutonsDUneLangue(){
270 $b = new Barre_outils($this->baseParamsBarre
);
271 $ico2 = $ico1 = array(
273 "name" => "test apparaissant si langue est le francais",
274 "className" => "outil_fr",
275 "openWith" => "[fr]",
276 "lang" => array("fr"),
279 $ico2['id'] = 'ico_fr2';
280 $ico2['lang'] = array("fr","en","es");
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'));
287 // verifier que ces nouveaux array()
288 // ne posent pas de problemes dans les recursions
290 $this->assertFalse($b->markupSet
[1]['display']);
291 $b->afficher('ico_fr1');
292 $this->assertTrue($b->markupSet
[1]['display']);
294 $b->afficher(array('ico_fr1','ico_fr2'));
295 $this->assertTrue($b->markupSet
[1]['display']);
297 // la langue est bien transmise au json
298 $json = $b->creer_json();
299 $this->assertPattern(',"lang":\[,', $json);
303 function testFonctionsJavacriptDansParametreNeDoitPasEtreEntreguillemetsDansJson(){
304 $b = new Barre_outils($this->baseParamsBarre
);
307 "name" => _T('barreoutils:barre_clean'),
308 "className" => "outil_clean",
309 // function doit etre echappe
310 "replaceWith" => 'function(markitup) { return markitup.selection.replace(/<(.*?)>/g, "") }',
313 $b->ajouterApres('header1', $clean);
314 $json = $b->creer_json();
315 // pas de :"function(... ..."
316 $this->assertPattern('/:function\(/',$json);
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"));
326 // mais uniquement en fin du fichier json
327 $this->assertPattern('/function dido\(/', $json);
330 function testAjouterFonctions(){
331 $b = new Barre_outils($this->baseParamsBarre
);
332 $b->ajouterFonction("function dido(){}");
333 $this->assertPattern('/function dido\(/', $b->functions
);
337 function squeletteTest(){
338 $sq = new SqueletteTest("SimpleTest");
339 $sq->addInsertHead();
341 <div class='formulaire_spip'>
343 <textarea name='texte' class='texte'></textarea>
350 function testPresenceBarreOutilPublique(){
351 include_spip('simpletest/browser');
352 include_spip('simpletest/web_tester');
354 $sq = $this->squeletteTest();
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());
362 #$this->assertPattern('/jquery\.markitup_pour_spip\.js/', $c);