From 42b48b4254dd0483f78e6104b0d361f163c9c931 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Fri, 20 Dec 2019 14:09:59 +0100 Subject: [PATCH] Add initializer class --- js/src/initializer.js | 67 +++++++++++++++++++ js/test/initiallizer.test.js | 124 +++++++++++++++++++++++++++++++++++ utils/getinit.php | 67 +++++++++++++++++++ 3 files changed, 258 insertions(+) create mode 100644 js/src/initializer.js create mode 100644 js/test/initiallizer.test.js create mode 100644 utils/getinit.php diff --git a/js/src/initializer.js b/js/src/initializer.js new file mode 100644 index 0000000..70511da --- /dev/null +++ b/js/src/initializer.js @@ -0,0 +1,67 @@ +/* + * μlogger + * + * Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net) + * + * This is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +import uAjax from './ajax.js'; +import uAuth from './auth.js'; +import uConfig from './config.js'; +import uLang from './lang.js'; + +/** + * @Class uInitializer + * @property {uAuth} auth + * @property {uConfig} config + * @property {uLang} lang + */ +export class uInitializer { + + constructor() { + this.auth = new uAuth(); + this.config = new uConfig(); + this.lang = new uLang(); + } + + /** + * @return {Promise} + */ + initialize() { + return uAjax.get('utils/getinit.php').then((_data) => { + if (!_data || !_data.auth || !_data.config || !_data.lang) { + throw new Error('Corrupted initialization data'); + } + this.auth.load(_data.auth); + this.config.load(_data.config); + this.lang.init(this.config, _data.lang); + }); + } + + static waitForDom() { + return new Promise((resolve) => { + if (document.readyState === 'complete' || document.readyState === 'interactive') { + setTimeout(resolve, 1); + } else { + document.addEventListener('DOMContentLoaded', resolve); + } + }); + } +} + +export const initializer = new uInitializer(); +export const config = initializer.config; +export const lang = initializer.lang; +export const auth = initializer.auth; diff --git a/js/test/initiallizer.test.js b/js/test/initiallizer.test.js new file mode 100644 index 0000000..0f83ef4 --- /dev/null +++ b/js/test/initiallizer.test.js @@ -0,0 +1,124 @@ +/* + * μlogger + * + * Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net) + * + * This is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +import uAjax from '../src/ajax.js'; +import uAuth from '../src/auth.js'; +import uConfig from '../src/config.js'; +import { uInitializer } from '../src/initializer.js'; +import uLang from '../src/lang.js'; + +describe('Initializer tests', () => { + + let initializer; + let data; + + beforeEach(() => { + data = { + auth: {}, + config: {}, + lang: {} + }; + initializer = new uInitializer(); + spyOn(initializer.auth, 'load'); + spyOn(initializer.config, 'load'); + spyOn(initializer.lang, 'init'); + spyOn(uAjax, 'get').and.returnValue(Promise.resolve(data)); + }); + + it('should create instance', () => { + expect(initializer.auth).toBeInstanceOf(uAuth); + expect(initializer.config).toBeInstanceOf(uConfig); + expect(initializer.lang).toBeInstanceOf(uLang); + }); + + it('should load data from server', (done) => { + // when + initializer.initialize().then(() => { + // then + expect(uAjax.get).toHaveBeenCalledWith('utils/getinit.php'); + expect(initializer.auth.load).toHaveBeenCalledWith(data.auth); + expect(initializer.config.load).toHaveBeenCalledWith(data.config); + expect(initializer.lang.init).toHaveBeenCalledWith(initializer.config, data.lang); + done(); + }).catch((e) => done.fail(`reject callback called (${e})`)); + }); + + it('should throw error on missing data.config', (done) => { + // given + delete data.config; + // when + initializer.initialize().then(() => { + // then + done.fail('resolve callback called'); + }).catch((e) => { + expect(e).toEqual(jasmine.any(Error)); + done(); + }); + }); + + it('should throw error on missing data.auth', (done) => { + // given + delete data.auth; + // when + initializer.initialize().then(() => { + // then + done.fail('resolve callback called'); + }).catch((e) => { + expect(e).toEqual(jasmine.any(Error)); + done(); + }); + }); + + it('should throw error on missing data.lang', (done) => { + // given + delete data.lang; + // when + initializer.initialize().then(() => { + // then + done.fail('resolve callback called'); + }).catch((e) => { + expect(e).toEqual(jasmine.any(Error)); + done(); + }); + }); + + it('should resolve on DOMContentLoaded event', (done) => { + // given + spyOnProperty(document, 'readyState').and.returnValue('loading'); + // when + uInitializer.waitForDom().then(() => { + // then + console.log(document.readyState); + done(); + }).catch((e) => done.fail(`reject callback called (${e})`)); + + document.dispatchEvent(new Event('DOMContentLoaded')); + }); + + it('should resolve on DOM ready', (done) => { + // given + spyOnProperty(document, 'readyState').and.returnValue('complete'); + // when + uInitializer.waitForDom().then(() => { + // then + done(); + }).catch((e) => done.fail(`reject callback called (${e})`)); + }); + +}); diff --git a/utils/getinit.php b/utils/getinit.php new file mode 100644 index 0000000..7d6eb4b --- /dev/null +++ b/utils/getinit.php @@ -0,0 +1,67 @@ +. + */ + +require_once(dirname(__DIR__) . "/helpers/auth.php"); +require_once(ROOT_DIR . "/helpers/config.php"); +require_once(ROOT_DIR . "/helpers/lang.php"); + +$auth = new uAuth(); +$langStrings = (new uLang(uConfig::$lang))->getStrings(); + +$result = []; +$resultAuth = [ + "isAdmin" => $auth->isAdmin(), + "isAuthenticated" => $auth->isAuthenticated() +]; +if ($auth->isAuthenticated()) { + $resultAuth["userId"] = $auth->user->id; + $resultAuth["userLogin"] = $auth->user->login; +} + +$resultConfig = [ + "interval" => uConfig::$interval, + "units" => uConfig::$units, + "lang" => uConfig::$lang, + "mapApi" => uConfig::$mapapi, + "gkey" => uConfig::$gkey, + "initLatitude" => uConfig::$init_latitude, + "initLongitude" => uConfig::$init_longitude, + "passRegex" => uConfig::passRegex(), + "strokeWeight" => uConfig::$strokeWeight, + "strokeColor" => uConfig::$strokeColor, + "strokeOpacity" => uConfig::$strokeOpacity, + "olLayers" => [] +]; +foreach (uConfig::$ol_layers as $key => $val) { + $resultConfig["olLayers"][$key] = $val; +} + +$resultLang = []; +foreach ($langStrings as $key => $val) { + $resultLang[$key] = $val; +} + +$result["auth"] = $resultAuth; +$result["config"] = $resultConfig; +$result["lang"] = $resultLang; + +header("Content-type: application/json"); +echo json_encode($result); + +?>