aboutsummaryrefslogtreecommitdiff
path: root/js/tests/ElggLibTest.js
blob: c53c6331dffe5c6ba62e85145c59c9545e99e5ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 * Test basic elgg library functions
 */
ElggLibTest = TestCase("ElggLibTest");

ElggLibTest.prototype.testGlobal = function() {
	assertTrue(window === elgg.global);
};

ElggLibTest.prototype.testAssertTypeOf = function() {
	[//Valid inputs
	    ['string', ''],
        ['object', {}],
        ['boolean', true],
        ['boolean', false],
        ['undefined', undefined],
        ['number', 0],
        ['function', elgg.nullFunction]
    ].forEach(function(args) {
		assertNoException(function() {
			elgg.assertTypeOf.apply(undefined, args);
		});
	});

	[//Invalid inputs
        ['function', {}],
        ['object', elgg.nullFunction]
    ].forEach(function() {
		assertException(function(args) {
			elgg.assertTypeOf.apply(undefined, args);
		});
	});
};

ElggLibTest.prototype.testProvideDoesntClobber = function() {
	elgg.provide('foo.bar.baz');

	foo.bar.baz.oof = "test";

	elgg.provide('foo.bar.baz');

	assertEquals("test", foo.bar.baz.oof);
};

/**
 * Try requiring bogus input
 */
ElggLibTest.prototype.testRequire = function () {
	assertException(function(){ elgg.require(''); });
	assertException(function(){ elgg.require('garbage'); });
	assertException(function(){ elgg.require('gar.ba.ge'); });

	assertNoException(function(){
		elgg.require('jQuery');
		elgg.require('elgg');
		elgg.require('elgg.config');
		elgg.require('elgg.security');
	});
};

ElggLibTest.prototype.testInherit = function () {
	function Base() {}
	function Child() {}

	elgg.inherit(Child, Base);

	assertInstanceOf(Base, new Child());
	assertEquals(Child, Child.prototype.constructor);
};

ElggLibTest.prototype.testNormalizeUrl = function() {
	elgg.config.wwwroot = "http://elgg.org/";

	[
		['', elgg.config.wwwroot],
		['test', elgg.config.wwwroot + 'test'],
		['http://example.com', 'http://example.com'],
		['https://example.com', 'https://example.com'],
		['http://example-time.com', 'http://example-time.com'],
		['//example.com', '//example.com'],

		['ftp://example.com/file', 'ftp://example.com/file'],
		['mailto:brett@elgg.org', 'mailto:brett@elgg.org'],
		['javascript:alert("test")', 'javascript:alert("test")'],
		['app://endpoint', 'app://endpoint'],

		['example.com', 'http://example.com'],
		['example.com/subpage', 'http://example.com/subpage'],

		['page/handler', elgg.config.wwwroot + 'page/handler'],
		['page/handler?p=v&p2=v2', elgg.config.wwwroot + 'page/handler?p=v&p2=v2'],
		['mod/plugin/file.php', elgg.config.wwwroot + 'mod/plugin/file.php'],
		['mod/plugin/file.php?p=v&p2=v2', elgg.config.wwwroot + 'mod/plugin/file.php?p=v&p2=v2'],
		['rootfile.php', elgg.config.wwwroot + 'rootfile.php'],
		['rootfile.php?p=v&p2=v2', elgg.config.wwwroot + 'rootfile.php?p=v&p2=v2'],

		['/page/handler', elgg.config.wwwroot + 'page/handler'],
		['/page/handler?p=v&p2=v2', elgg.config.wwwroot + 'page/handler?p=v&p2=v2'],
		['/mod/plugin/file.php', elgg.config.wwwroot + 'mod/plugin/file.php'],
		['/mod/plugin/file.php?p=v&p2=v2', elgg.config.wwwroot + 'mod/plugin/file.php?p=v&p2=v2'],
		['/rootfile.php', elgg.config.wwwroot + 'rootfile.php'],
		['/rootfile.php?p=v&p2=v2', elgg.config.wwwroot + 'rootfile.php?p=v&p2=v2'],

	].forEach(function(args) {
		assertEquals(args[1], elgg.normalize_url(args[0]));
	});
};