1 | var chai = require('chai'); |
||
2 | var sinonChai = require('sinon-chai') |
||
3 | var expect = chai.expect |
||
4 | chai.use(sinonChai) |
||
5 | var sinon = require('sinon'); |
||
6 | var path = require('path'); |
||
7 | var fse = require('fs-extra'); |
||
8 | |||
9 | var config = require('../../../src/cli').config |
||
10 | config.set({root: path.join(process.cwd(), 'test', 'fixtures')}) |
||
11 | |||
12 | var abeExtend = require('../../../src/cli').abeExtend; |
||
13 | var cmsData = require('../../../src/cli').cmsData; |
||
14 | var cmsTemplates = require('../../../src/cli').cmsTemplates; |
||
15 | var coreUtils = require('../../../src/cli').coreUtils; |
||
16 | var Manager = require('../../../src/cli').Manager; |
||
17 | |||
18 | describe('cmsTemplates', function() { |
||
19 | before( function(done) { |
||
20 | Manager.instance.init() |
||
21 | .then(function () { |
||
22 | this.fixture = { |
||
23 | slug: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'slug.html'), 'utf-8'), |
||
24 | article: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article.html'), 'utf-8'), |
||
25 | local: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'local.html'), 'utf-8'), |
||
26 | articleSingleAbe: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-single-abe.html'), 'utf-8'), |
||
27 | articleRequest: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-request.html'), 'utf-8'), |
||
28 | template: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'import.html'), 'utf-8'), |
||
29 | articleEach: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-each-abe.html'), 'utf-8'), |
||
30 | articlePrecontrib: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-precontribution.html'), 'utf-8'), |
||
31 | templateKeys: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-keys.html'), 'utf-8'), |
||
32 | templatePaths: path.join(process.cwd(), 'test', 'fixtures', 'templates', 'templates/article.html'), |
||
33 | structurePaths: path.join(process.cwd(), 'test', 'fixtures', 'templates', 'templates/structure/0-1'), |
||
34 | import: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'import.html'), 'utf-8'), |
||
35 | pathTemplates: path.join(process.cwd(), 'test', 'fixtures', 'templates'), |
||
36 | pathPartials: path.join(process.cwd(), 'test', 'fixtures', 'partials'), |
||
37 | count: 0 |
||
38 | } |
||
39 | done() |
||
40 | |||
41 | }.bind(this)) |
||
42 | }); |
||
43 | |||
44 | /** |
||
45 | * cmsTemplates.template.getTemplatesAndPartials |
||
46 | * |
||
47 | */ |
||
48 | it('cmsTemplates.template.getTemplatesAndPartials()', function(done) { |
||
49 | |||
50 | cmsTemplates.template.getTemplatesAndPartials(this.fixture.pathTemplates, this.fixture.pathPartials) |
||
51 | .then((templatesList) => { |
||
52 | chai.expect(templatesList.length).to.be.equal(29); |
||
53 | done() |
||
54 | }) |
||
55 | }); |
||
56 | |||
57 | /** |
||
58 | * |
||
59 | * |
||
60 | */ |
||
61 | it('cmsTemplates.template.addOrder()', function() { |
||
62 | // stub |
||
63 | var sinonInstance = sinon.sandbox.create(); |
||
64 | var getAttr = sinonInstance.stub(cmsData.regex, 'getAttr'); |
||
65 | getAttr.returns(null) |
||
66 | |||
67 | // test |
||
68 | var html = cmsTemplates.template.addOrder(this.fixture.articleSingleAbe) |
||
69 | chai.expect(html).to.not.be.equal(this.fixture.articleSingleAbe); |
||
70 | chai.expect(html.indexOf('order')).to.above(-1); |
||
71 | |||
72 | // unstub |
||
73 | sinon.assert.calledOnce(cmsData.regex.getAttr) |
||
74 | cmsData.regex.getAttr.restore() |
||
75 | }); |
||
76 | |||
77 | /** |
||
78 | * cmsTemplates.template.getStructureAndTemplates |
||
79 | * |
||
80 | */ |
||
81 | it('cmsTemplates.template.getStructureAndTemplates()', function() { |
||
82 | // stub |
||
83 | var sinonInstance = sinon.sandbox.create(); |
||
84 | var stubGetFoldersSync = sinonInstance.stub(coreUtils.file, 'getFoldersSync'); |
||
85 | stubGetFoldersSync.returns({path: this.fixture.structurePaths, folders: []}) |
||
86 | var stubGetFilesSync = sinonInstance.stub(coreUtils.file, 'getFilesSync'); |
||
87 | stubGetFilesSync.returns([this.fixture.templatePaths]) |
||
88 | |||
89 | // test |
||
90 | var res = cmsTemplates.template.getStructureAndTemplates() |
||
91 | chai.expect(res.templates.length).to.be.equal(1); |
||
92 | |||
93 | // unstub |
||
94 | sinon.assert.calledOnce(coreUtils.file.getFoldersSync) |
||
95 | coreUtils.file.getFoldersSync.restore() |
||
96 | sinon.assert.calledOnce(coreUtils.file.getFilesSync) |
||
97 | coreUtils.file.getFilesSync.restore() |
||
98 | }); |
||
99 | |||
100 | /** |
||
101 | * getAbeImport |
||
102 | * |
||
103 | */ |
||
104 | it('cmsTemplates.template.getAbeImport()', function() { |
||
105 | var res = cmsTemplates.template.getAbeImport(this.fixture.template) |
||
106 | chai.expect(res).to.have.length(1); |
||
107 | }); |
||
108 | |||
109 | /** |
||
110 | * translate |
||
111 | * |
||
112 | */ |
||
113 | it('cmsTemplates.template.translate()', function() { |
||
114 | // stub |
||
115 | var sinonInstance = sinon.sandbox.create(); |
||
116 | var getAttr = sinonInstance.stub(cmsData.regex, 'getAttr', function (math, type) { |
||
117 | if (type === 'locale') { |
||
118 | return 'gb' |
||
119 | } |
||
120 | if (type === 'source') { |
||
0 ignored issues
–
show
|
|||
121 | return 'test' |
||
122 | } |
||
123 | }); |
||
124 | var escapeTextToRegex = sinonInstance.stub(cmsData.regex, 'escapeTextToRegex'); |
||
125 | escapeTextToRegex.returns(new RegExp(this.fixture.local, 'g')) |
||
126 | |||
127 | // test |
||
128 | var res = cmsTemplates.template.translate(this.fixture.local) |
||
129 | chai.expect(res.indexOf('i18nAbe')).to.above(-1); |
||
130 | |||
131 | // unstub |
||
132 | cmsData.regex.getAttr.restore() |
||
133 | sinon.assert.calledOnce(cmsData.regex.escapeTextToRegex) |
||
134 | cmsData.regex.escapeTextToRegex.restore() |
||
135 | }); |
||
136 | |||
137 | /** |
||
138 | * getVariablesInWhere |
||
139 | * |
||
140 | */ |
||
141 | it('cmsTemplates.template.getVariablesInWhere()', function() { |
||
142 | // stub |
||
143 | |||
144 | // test |
||
145 | var res = cmsTemplates.template.getVariablesInWhere({left: {column: 'title'}, right: {value: "test"}, operator: 'AND'}) |
||
146 | chai.expect(res.length).to.above(-1); |
||
147 | |||
148 | // unstub |
||
149 | }); |
||
150 | |||
151 | /** |
||
152 | * recurseWhereVariables |
||
153 | * |
||
154 | */ |
||
155 | it('cmsTemplates.template.recurseWhereVariables()', function() { |
||
156 | // stub |
||
157 | |||
158 | // test |
||
159 | var res = cmsTemplates.template.recurseWhereVariables({left: {column: 'title'}, right: {value: "test"}}) |
||
160 | chai.expect(res.length).to.above(-1); |
||
161 | |||
162 | // unstub |
||
163 | }); |
||
164 | |||
165 | /** |
||
166 | * getTemplatesTexts |
||
167 | * |
||
168 | */ |
||
169 | it('cmsTemplates.template.getTemplatesTexts()', function(done) { |
||
170 | // stub |
||
171 | var sinonInstance = sinon.sandbox.create(); |
||
172 | var readFileSync = sinonInstance.stub(fse, 'readFileSync'); |
||
173 | readFileSync.returns("test") |
||
174 | |||
175 | // test |
||
176 | cmsTemplates.template.getTemplatesTexts(['test']) |
||
177 | .then(function (res) { |
||
178 | chai.expect(res[0].name).to.be.equal('test'); |
||
179 | |||
180 | // unstub |
||
181 | sinon.assert.calledOnce(fse.readFileSync) |
||
182 | fse.readFileSync.restore() |
||
183 | done() |
||
184 | }) |
||
185 | }); |
||
186 | |||
187 | /** |
||
188 | * includePartials |
||
189 | * |
||
190 | */ |
||
191 | it('cmsTemplates.template.includePartials()', function() { |
||
192 | // stub |
||
193 | var sinonInstance = sinon.sandbox.create(); |
||
194 | var stubReadFileSync = sinonInstance.stub(fse, 'readFileSync'); |
||
195 | stubReadFileSync.returns("test") |
||
196 | var stubGetAbeImport = sinonInstance.stub(cmsTemplates.template, 'getAbeImport', function () { |
||
0 ignored issues
–
show
|
|||
197 | if (this.fixture.count === 0) { |
||
198 | this.fixture.count++ |
||
199 | return [this.fixture.import] |
||
200 | } |
||
201 | return [] |
||
202 | }.bind(this)); |
||
203 | var stubGetAll = sinonInstance.stub(cmsData.attributes, 'getAll'); |
||
204 | stubGetAll.returns({file: 'import.html'}) |
||
205 | var stubEscapeTextToRegex = sinonInstance.stub(cmsData.regex, 'escapeTextToRegex'); |
||
206 | stubEscapeTextToRegex.returns(new RegExp(this.fixture.import, 'g')) |
||
207 | var stubFile = sinonInstance.stub(coreUtils.file, 'exist', function () { |
||
0 ignored issues
–
show
|
|||
208 | return true |
||
209 | }.bind(this)); |
||
0 ignored issues
–
show
|
|||
210 | |||
211 | // test |
||
212 | var template = cmsTemplates.template.includePartials(this.fixture.template) |
||
213 | chai.expect(template).to.be.equal("test"); |
||
214 | |||
215 | // unstub |
||
216 | sinon.assert.calledOnce(fse.readFileSync) |
||
217 | fse.readFileSync.restore() |
||
218 | sinon.assert.calledTwice(cmsTemplates.template.getAbeImport) |
||
219 | cmsTemplates.template.getAbeImport.restore() |
||
220 | sinon.assert.calledOnce(cmsData.attributes.getAll) |
||
221 | cmsData.attributes.getAll.restore() |
||
222 | sinon.assert.calledOnce(coreUtils.file.exist) |
||
223 | coreUtils.file.exist.restore() |
||
224 | sinon.assert.calledOnce(cmsData.regex.escapeTextToRegex) |
||
225 | cmsData.regex.escapeTextToRegex.restore() |
||
226 | }); |
||
227 | |||
228 | it('cmsTemplates.template.includePartials()', function() { |
||
229 | // stub |
||
230 | var sinonInstance = sinon.sandbox.create(); |
||
231 | var stubReadFileSync = sinonInstance.stub(fse, 'readFileSync'); |
||
232 | stubReadFileSync.returns("test") |
||
233 | |||
234 | var template = cmsTemplates.template.includePartials("{{abe type='import' file='{{test}}'}}", {"test" : "test.html"}) |
||
235 | fse.readFileSync.restore() |
||
236 | chai.expect(template).to.be.equal("test") |
||
237 | }); |
||
238 | |||
239 | /** |
||
240 | * cmsTemplates.template.getTemplate |
||
241 | * |
||
242 | */ |
||
243 | it('cmsTemplates.template.getTemplate()', function() { |
||
244 | // stub |
||
245 | var sinonInstance = sinon.sandbox.create(); |
||
246 | var stubReadFileSync = sinonInstance.stub(fse, 'readFileSync'); |
||
247 | stubReadFileSync.returns(this.fixture.article) |
||
248 | var stubTrigger = sinonInstance.stub(abeExtend.hooks.instance, 'trigger', function (p1, p2) { |
||
0 ignored issues
–
show
|
|||
249 | return p2 |
||
250 | }) |
||
251 | var stubExist = sinonInstance.stub(coreUtils.file, 'exist'); |
||
252 | stubExist.returns(true) |
||
253 | var stubIncludePartials = sinonInstance.stub(cmsTemplates.template, 'includePartials'); |
||
254 | stubIncludePartials.returns(this.fixture.article) |
||
255 | var stubTranslate = sinonInstance.stub(cmsTemplates.template, 'translate'); |
||
256 | stubTranslate.returns(this.fixture.article) |
||
257 | var stubAddOrder = sinonInstance.stub(cmsTemplates.template, 'addOrder'); |
||
258 | stubAddOrder.returns(this.fixture.article) |
||
259 | |||
260 | var template = cmsTemplates.template.getTemplate('article') |
||
261 | chai.expect(template).to.be.equal(this.fixture.article); |
||
262 | |||
263 | sinon.assert.calledOnce(fse.readFileSync) |
||
264 | fse.readFileSync.restore() |
||
265 | sinon.assert.calledTwice(abeExtend.hooks.instance.trigger) |
||
266 | abeExtend.hooks.instance.trigger.restore() |
||
267 | sinon.assert.calledOnce(coreUtils.file.exist) |
||
268 | coreUtils.file.exist.restore() |
||
269 | sinon.assert.calledOnce(cmsTemplates.template.includePartials) |
||
270 | cmsTemplates.template.includePartials.restore() |
||
271 | sinon.assert.calledOnce(cmsTemplates.template.translate) |
||
272 | cmsTemplates.template.translate.restore() |
||
273 | sinon.assert.calledOnce(cmsTemplates.template.addOrder) |
||
274 | cmsTemplates.template.addOrder.restore() |
||
275 | }); |
||
276 | |||
277 | /** |
||
278 | * cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist |
||
279 | * |
||
280 | */ |
||
281 | it('cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist()', function() { |
||
282 | // stub |
||
283 | var sinonInstance = sinon.sandbox.create(); |
||
284 | var stubGetTagAbeWithType = sinonInstance.stub(cmsData.regex, 'getTagAbeWithType'); |
||
285 | stubGetTagAbeWithType.returns(null) |
||
286 | |||
287 | // test |
||
288 | var text = cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist(this.fixture.slug) |
||
289 | chai.expect(text).to.not.be.equal(this.fixture.slug); |
||
290 | |||
291 | // unstub |
||
292 | sinon.assert.calledOnce(cmsData.regex.getTagAbeWithType) |
||
293 | cmsData.regex.getTagAbeWithType.restore() |
||
294 | }); |
||
295 | |||
296 | /** |
||
297 | * cmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist |
||
298 | * |
||
299 | */ |
||
300 | it('cmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist()', function() { |
||
301 | // stub |
||
302 | var sinonInstance = sinon.sandbox.create(); |
||
303 | var stubGetTagAbeWithTab = sinonInstance.stub(cmsData.regex, 'getTagAbeWithTab'); |
||
304 | stubGetTagAbeWithTab.returns(null) |
||
305 | |||
306 | // test |
||
307 | var text = cmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist(this.fixture.slug) |
||
308 | chai.expect(text).to.not.be.equal(this.fixture.slug); |
||
309 | |||
310 | // unstub |
||
311 | sinon.assert.calledOnce(cmsData.regex.getTagAbeWithTab) |
||
312 | cmsData.regex.getTagAbeWithTab.restore() |
||
313 | }); |
||
314 | |||
315 | /** |
||
316 | * cmsTemplates.template.getAbePrecontribFromTemplates |
||
317 | * |
||
318 | */ |
||
319 | it('cmsTemplates.template.getAbePrecontribFromTemplates()', function() { |
||
320 | // stub |
||
321 | var sinonInstance = sinon.sandbox.create(); |
||
322 | var stubGetTagAbeWithTab = sinonInstance.stub(cmsData.regex, 'getTagAbeWithTab'); |
||
323 | stubGetTagAbeWithTab.returns(['{{abe type="slug" source="{{name}}"}}']) |
||
324 | |||
325 | var stubGetAll = sinonInstance.stub(cmsData.attributes, 'getAll'); |
||
326 | stubGetAll.returns({name: 'test.html'}) |
||
327 | |||
328 | var stubAddOrder = sinonInstance.stub(cmsTemplates.template, 'addOrder', function (text) { |
||
0 ignored issues
–
show
|
|||
329 | return text |
||
330 | }); |
||
331 | |||
332 | // test |
||
333 | var precontrib = cmsTemplates.template.getAbePrecontribFromTemplates([{name: 'slug', template: this.fixture.slug}]) |
||
334 | chai.expect(precontrib.fields[0]).to.not.be.undefined; |
||
0 ignored issues
–
show
|
|||
335 | |||
336 | // unstub |
||
337 | sinon.assert.calledOnce(cmsData.regex.getTagAbeWithTab) |
||
338 | cmsData.regex.getTagAbeWithTab.restore() |
||
339 | sinon.assert.calledOnce(cmsData.attributes.getAll) |
||
340 | cmsData.attributes.getAll.restore() |
||
341 | sinon.assert.calledOnce(cmsTemplates.template.addOrder) |
||
342 | cmsTemplates.template.addOrder.restore() |
||
343 | }); |
||
344 | |||
345 | /** |
||
346 | * cmsTemplates.template.getAbeSlugFromTemplates |
||
347 | * |
||
348 | */ |
||
349 | it('cmsTemplates.template.getAbeSlugFromTemplates()', function() { |
||
350 | // stub |
||
351 | var sinonInstance = sinon.sandbox.create(); |
||
352 | var setAbeSlugDefaultValueIfDoesntExist = sinonInstance.stub(cmsTemplates.template, 'setAbeSlugDefaultValueIfDoesntExist'); |
||
353 | setAbeSlugDefaultValueIfDoesntExist.returns(this.fixture.slug) |
||
354 | var getTagAbeWithType = sinonInstance.stub(cmsData.regex, 'getTagAbeWithType'); |
||
355 | getTagAbeWithType.returns(['{{abe type="slug" source="{{name}}"}}']) |
||
356 | var getAll = sinonInstance.stub(cmsData.attributes, 'getAll'); |
||
357 | getAll.returns({sourceString: 'test.html', name: 'test'}) |
||
358 | |||
359 | var slug = cmsTemplates.template.getAbeSlugFromTemplates([{name: 'slug', template: this.fixture.slug}]) |
||
360 | chai.expect(slug.slug).to.not.be.undefined; |
||
0 ignored issues
–
show
|
|||
361 | |||
362 | // unstub |
||
363 | sinon.assert.calledOnce(cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist) |
||
364 | cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist.restore() |
||
365 | sinon.assert.calledOnce(cmsData.regex.getTagAbeWithType) |
||
366 | cmsData.regex.getTagAbeWithType.restore() |
||
367 | sinon.assert.calledOnce(cmsData.attributes.getAll) |
||
368 | cmsData.attributes.getAll.restore() |
||
369 | }); |
||
370 | |||
371 | /** |
||
372 | * getTemplate |
||
373 | * |
||
374 | */ |
||
375 | it('cmsTemplates.template.execRequestColumns()', function() { |
||
376 | // stub |
||
377 | var sinonInstance = sinon.sandbox.create(); |
||
378 | var getTagAbeTypeRequest = sinonInstance.stub(cmsData.regex, 'getTagAbeTypeRequest'); |
||
379 | getTagAbeTypeRequest.returns([this.fixture.articleRequest]) |
||
380 | var getSourceType = sinonInstance.stub(cmsData.sql, 'getSourceType'); |
||
381 | getSourceType.returns('request') |
||
382 | var handleSqlRequest = sinonInstance.stub(cmsData.sql, 'handleSqlRequest'); |
||
383 | handleSqlRequest.returns({columns: ['title'], where: {left: 'title'}}) |
||
384 | var recurseWhereVariables = sinonInstance.stub(cmsTemplates.template, 'recurseWhereVariables'); |
||
385 | recurseWhereVariables.returns('title2') |
||
386 | |||
387 | // test |
||
388 | const pathTemplate = path.join(config.root, config.templates.url) |
||
0 ignored issues
–
show
|
|||
389 | var ar = cmsTemplates.template.execRequestColumns(this.fixture.templateKeys) |
||
390 | chai.expect(ar.indexOf('title')).to.be.above(-1); |
||
391 | |||
392 | // unstub |
||
393 | sinon.assert.calledOnce(cmsData.regex.getTagAbeTypeRequest) |
||
394 | cmsData.regex.getTagAbeTypeRequest.restore() |
||
395 | sinon.assert.calledOnce(cmsData.sql.getSourceType) |
||
396 | cmsData.sql.getSourceType.restore() |
||
397 | sinon.assert.calledOnce(cmsData.sql.handleSqlRequest) |
||
398 | cmsData.sql.handleSqlRequest.restore() |
||
399 | sinon.assert.calledOnce(cmsTemplates.template.recurseWhereVariables) |
||
400 | cmsTemplates.template.recurseWhereVariables.restore() |
||
401 | }); |
||
402 | |||
403 | /** |
||
404 | * getTemplate |
||
405 | * |
||
406 | */ |
||
407 | it('cmsTemplates.template.getAbeRequestWhereKeysFromTemplates()', function(done) { |
||
408 | // stub |
||
409 | var sinonInstance = sinon.sandbox.create(); |
||
410 | var execRequestColumns = sinonInstance.stub(cmsTemplates.template, 'execRequestColumns'); |
||
411 | execRequestColumns.returns(['title']) |
||
412 | |||
413 | // test |
||
414 | const pathTemplate = path.join(config.root, config.templates.url) |
||
0 ignored issues
–
show
|
|||
415 | cmsTemplates.template.getAbeRequestWhereKeysFromTemplates([this.fixture.articleRequest]) |
||
416 | .then(function (ar) { |
||
417 | chai.expect(ar.indexOf('title')).to.be.above(-1); |
||
418 | |||
419 | // unstub |
||
420 | sinon.assert.calledOnce(cmsTemplates.template.execRequestColumns) |
||
421 | cmsTemplates.template.execRequestColumns.restore() |
||
422 | done() |
||
423 | }) |
||
424 | }); |
||
425 | |||
426 | /** |
||
427 | * cmsTemplates.encodeAbeTagAsComment |
||
428 | * |
||
429 | */ |
||
430 | it('cmsTemplates.encodeAbeTagAsComment()', function() { |
||
431 | var txt = cmsTemplates.encodeAbeTagAsComment(this.fixture.articleEach); |
||
432 | chai.expect(txt.indexOf('{')).to.equal(-1); |
||
433 | }); |
||
434 | }); |
||
435 |
This check looks for functions where a
return
statement is found in some execution paths, but not in all.Consider this little piece of code
The function
isBig
will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly returnundefined
.This behaviour may not be what you had intended. In any case, you can add a
return undefined
to the other execution path to make the return value explicit.