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",
36 $p = $this->baseParamsBarre
;
37 $p['markupSet'][1] = array(
39 "name" => _T('barreoutils:barre_couleur'),
41 "className" => "outil_couleur",
42 "openWith" => '[color=[![Color]!]]',
43 "closeWith" => '[/color]',
47 "id" => "couleur_jaune",
49 "openWith" => '[color=yellow]',
50 "closeWith" => '[/color]',
51 "className" => "outil_couleur",
55 "id" => "couleur_orange",
57 "openWith" => '[color=orange]',
58 "closeWith" => '[/color]',
59 "className" => "outil_couleur",
63 "id" => "couleur_rouge",
65 "openWith" => '[color=red]',
66 "closeWith" => '[/color]',
67 "className" => "outil_couleur",
72 $this->baseParamsBarreEtendue
= $p;
75 // avant chaque appel de fonction test
80 // apres chaque appel de fonction test
86 function testInitialisationBarre() {
87 // parametres inseres a leur bonne place
88 $b = new Barre_outils($this->baseParamsBarre
);
89 $this->assertEqual('spip', $b->nameSpace);
90 $this->assertEqual('header1', $b->markupSet
[0]['id']);
91 $this->assertEqual(7, count($b->markupSet
[0]));
94 function testInitialisationBarreEtendue() {
95 // parametres inseres a leur bonne place,
96 // meme quand il y a des sous-menu d'icones
97 $b = new Barre_outils($this->baseParamsBarreEtendue
);
98 $this->assertEqual('spip', $b->nameSpace);
99 $this->assertEqual('header1', $b->markupSet
[0]['id']);
100 $this->assertEqual(7, count($b->markupSet
[0]));
101 $this->assertEqual('couleurs', $b->markupSet
[1]['id']);
102 $this->assertEqual(3, count($b->markupSet
[1]['dropMenu']));
105 function testOptionsIncorrectesNonIncluses() {
106 $p = $this->baseParamsBarre
;
107 $p['fausseVariable'] = "je ne dois pas m'installer";
108 $p['markupSet'][0]['fauxParam'] = "je ne dois pas m'installer";
109 $b = new Barre_outils($p);
110 $this->assertEqual('spip', $b->nameSpace);
112 $this->expectError(new PatternExpectation("/Undefined property: Barre_outils::\\\$fausseVariable/i"));
115 $this->expectError(new PatternExpectation("/Undefined index: fauxParam/i"));
116 $b->markupSet
[0]['fauxParam'];
118 $this->assertEqual(7, count($b->markupSet
[0]));
121 function testRecuperationDeParametreAvecGet() {
122 // trouver des id de premier niveau
123 $p = $this->baseParamsBarre
;
124 $b = new Barre_outils($p);
125 $this->assertEqual($b->get('header1'), $p['markupSet'][0]);
127 // trouver des id de second niveau
128 $p = $this->baseParamsBarreEtendue
;
129 $b = new Barre_outils($p);
130 $this->assertEqual($b->get('header1'), $p['markupSet'][0]);
131 $this->assertEqual($b->get('couleurs'), $p['markupSet'][1]);
132 $this->assertEqual($b->get('couleur_jaune'), $p['markupSet'][1]['dropMenu'][0]);
133 $this->assertEqual($b->get('couleur_orange'), $p['markupSet'][1]['dropMenu'][1]);
134 $this->assertEqual($b->get('couleur_rouge'), $p['markupSet'][1]['dropMenu'][2]);
136 // ne pas trouver d'id inconnu
137 $this->assertFalse($b->get('je_nexiste_pas'));
140 function testModificationDeParametresAvecSet() {
141 $p = $this->baseParamsBarre
;
142 $b = new Barre_outils($p);
143 $p['markupSet'][0]['name'] = 'New';
144 $r = $p['markupSet'][0];
145 $x = $b->set('header1', array("name" => "New"));
147 $this->assertEqual($r, $x); // set retourne la chaine modifiee complete
148 $this->assertEqual($r, $b->get('header1'));
150 // on ne peut ajouter de mauvais parametres
151 $x = $b->set('header1', array("Je Suis Pas Bon" => "No no no"));
152 $this->assertEqual($r, $x); // set retourne la chaine modifiee complete
153 $this->assertEqual($r, $b->get('header1'));
156 function testAjoutDeParametresApres() {
157 $b = new Barre_outils($this->baseParamsBarre
);
158 $p = $this->baseParamsBarreEtendue
;
160 // ajoutons la couleur apres
161 $b->ajouterApres('header1', $p['markupSet'][1]);
162 $this->assertEqual(2, count($b->markupSet
)); // 2 boutons de premier niveau maintenant
163 $this->assertEqual($b->get('couleurs'), $p['markupSet'][1]); // get renvoie bien le bon ajout
164 $this->assertEqual($b->markupSet
[1], $p['markupSet'][1]); // et l'ajout est au bon endroit
166 // ajoutons une couleur dans l'ajout
167 $coul = $p['markupSet'][1]['dropMenu'][0];
168 $coul['id'] = 'couleur_violette';
169 $b->ajouterApres('couleur_orange', $coul);
170 $this->assertEqual(4, count($b->markupSet
[1]['dropMenu'])); // sous boutons
171 $this->assertEqual($b->get('couleur_violette'), $coul);
172 $this->assertEqual($b->markupSet
[1]['dropMenu'][2], $coul); // insertion au bon endroit
174 // ajoutons un header2 encore apres
175 $p['markupSet'][0]['id'] = 'header2';
176 $b->ajouterApres('couleurs', $p['markupSet'][0]);
177 $this->assertEqual(3, count($b->markupSet
));
178 $this->assertEqual($b->get('header2'), $p['markupSet'][0]);
179 $this->assertEqual($b->markupSet
[2], $p['markupSet'][0]);
182 function testAjoutDeParametresAvant() {
183 $b = new Barre_outils($this->baseParamsBarre
);
184 $p = $this->baseParamsBarreEtendue
;
186 // ajoutons la couleur apres
187 $b->ajouterAvant('header1', $p['markupSet'][1]);
188 $this->assertEqual(2, count($b->markupSet
)); // 2 boutons de premier niveau maintenant
189 $this->assertEqual($b->get('couleurs'), $p['markupSet'][1]); // get renvoie bien le bon ajout
190 $this->assertEqual($b->markupSet
[0], $p['markupSet'][1]); // et l'ajout est au bon endroit
192 // ajoutons une couleur dans l'ajout
193 $coul = $p['markupSet'][1]['dropMenu'][0];
194 $coul['id'] = 'couleur_violette';
195 $b->ajouterAvant('couleur_orange', $coul);
196 $this->assertEqual(4, count($b->markupSet
[0]['dropMenu'])); // sous boutons
197 $this->assertEqual($b->get('couleur_violette'), $coul);
198 $this->assertEqual($b->markupSet
[0]['dropMenu'][1], $coul); // insertion au bon endroit
200 // ajoutons un header2 avant le 1
201 $p['markupSet'][0]['id'] = 'header2';
202 $b->ajouterAvant('header1', $p['markupSet'][0]);
203 $this->assertEqual(3, count($b->markupSet
));
204 $this->assertEqual($b->get('header2'), $p['markupSet'][0]);
205 $this->assertEqual($b->markupSet
[1], $p['markupSet'][0]);
208 function testAfficherEtCacher() {
209 $b = new Barre_outils($this->baseParamsBarre
);
210 $b->cacher('header1');
211 $this->assertFalse($b->markupSet
[0]['display']);
212 $b->afficher('header1');
213 $this->assertTrue($b->markupSet
[0]['display']);
216 function testAfficherEtCacherTout() {
217 $b = new Barre_outils($this->baseParamsBarreEtendue
);
219 $this->assertFalse($b->markupSet
[0]['display']);
220 $this->assertFalse($b->markupSet
[1]['dropMenu'][0]['display']);
223 $this->assertTrue($b->markupSet
[0]['display']);
224 $this->assertTrue($b->markupSet
[1]['dropMenu'][0]['display']);
227 function testAfficherEtCacherPlusieursBoutons() {
228 $b = new Barre_outils($this->baseParamsBarreEtendue
);
229 $b->cacher(array('header1', 'couleur_jaune'));
230 $this->assertFalse($b->markupSet
[0]['display']);
231 $this->assertFalse($b->markupSet
[1]['dropMenu'][0]['display']);
232 $this->assertTrue($b->markupSet
[1]['dropMenu'][1]['display']);
235 $b->afficher(array('header1', 'couleur_jaune'));
236 $this->assertTrue($b->markupSet
[0]['display']);
237 $this->assertTrue($b->markupSet
[1]['dropMenu'][0]['display']);
238 $this->assertFalse($b->markupSet
[1]['dropMenu'][1]['display']);
241 function testSetAvecIdVideNeDoitRienModifier() {
242 $b = new Barre_outils($this->baseParamsBarreEtendue
);
243 $b->set(array(), array('display' => false));
244 $this->assertTrue($b->markupSet
[0]['display']);
245 $this->assertTrue($b->markupSet
[1]['dropMenu'][0]['display']);
248 function testSetAvecIdArrayDoitModifTousLesIds() {
249 $b = new Barre_outils($this->baseParamsBarreEtendue
);
250 $b->set(array('header1', 'couleur_jaune'), array('display' => false));
251 $this->assertFalse($b->markupSet
[0]['display']);
252 $this->assertFalse($b->markupSet
[1]['dropMenu'][0]['display']);
253 $this->assertTrue($b->markupSet
[1]['dropMenu'][1]['display']);
256 function testCreerJson() {
257 $b = new Barre_outils($this->baseParamsBarre
);
258 $b->ajouterApres('header1', array(
259 "id" => 'Caracteres decodes',
260 "name" => "étrange",
261 "className" => "outil_fr",
262 "openWith" => "[fr]",
265 $json = $b->creer_json();
266 $this->assertPattern(',barre_outils_spip = {,', $json);
267 $this->assertPattern(',\[{"name":",', $json);
268 $this->assertNoPattern(',eacute;,', $json);
271 function testBoutonsDUneLangue() {
272 $b = new Barre_outils($this->baseParamsBarre
);
273 $ico2 = $ico1 = array(
275 "name" => "test apparaissant si langue est le francais",
276 "className" => "outil_fr",
277 "openWith" => "[fr]",
278 "lang" => array("fr"),
281 $ico2['id'] = 'ico_fr2';
282 $ico2['lang'] = array("fr", "en", "es");
284 $b->ajouterApres('header1', $ico1);
285 $b->ajouterApres('ico_fr1', $ico2);
286 $this->assertEqual($ico1, $b->get('ico_fr1'));
287 $this->assertEqual($ico2, $b->get('ico_fr2'));
289 // verifier que ces nouveaux array()
290 // ne posent pas de problemes dans les recursions
292 $this->assertFalse($b->markupSet
[1]['display']);
293 $b->afficher('ico_fr1');
294 $this->assertTrue($b->markupSet
[1]['display']);
296 $b->afficher(array('ico_fr1', 'ico_fr2'));
297 $this->assertTrue($b->markupSet
[1]['display']);
299 // la langue est bien transmise au json
300 $json = $b->creer_json();
301 $this->assertPattern(',"lang":\[,', $json);
305 function testFonctionsJavacriptDansParametreNeDoitPasEtreEntreguillemetsDansJson() {
306 $b = new Barre_outils($this->baseParamsBarre
);
309 "name" => _T('barreoutils:barre_clean'),
310 "className" => "outil_clean",
311 // function doit etre echappe
312 "replaceWith" => 'function(markitup) { return markitup.selection.replace(/<(.*?)>/g, "") }',
315 $b->ajouterApres('header1', $clean);
316 $json = $b->creer_json();
317 // pas de :"function(... ..."
318 $this->assertPattern('/:function\(/', $json);
321 function testParametreFunctionsDansJson() {
322 $b = new Barre_outils($this->baseParamsBarre
);
323 $b->functions
= "function dido(){}";
324 $json = $b->creer_json();
325 // function n'est plus dans la barre
326 $this->expectError(new PatternExpectation("/Undefined property: Barre_outils::\\\$functions/i"));
328 // mais uniquement en fin du fichier json
329 $this->assertPattern('/function dido\(/', $json);
332 function testAjouterFonctions() {
333 $b = new Barre_outils($this->baseParamsBarre
);
334 $b->ajouterFonction("function dido(){}");
335 $this->assertPattern('/function dido\(/', $b->functions
);
339 function squeletteTest(){
340 $sq = new SqueletteTest("SimpleTest");
341 $sq->addInsertHead();
343 <div class='formulaire_spip'>
345 <textarea name='texte' class='texte'></textarea>
352 function testPresenceBarreOutilPublique(){
353 include_spip('simpletest/browser');
354 include_spip('simpletest/web_tester');
356 $sq = $this->squeletteTest();
358 $browser = &new SimpleBrowser();
359 $browser->get($f=$this->urlTestCode($sq->code()));
360 $browser->setField('texte', 'me');
361 $this->dump($browser->getField('texte'));
362 $this->dump($browser->getContent());
364 #$this->assertPattern('/jquery\.markitup_pour_spip\.js/', $c);