Apply lowerCamelCase to files for constructors as well.
[mediawiki.git] / tests / qunit / suites / resources / mediawiki / mediawiki.title.js
blob9a765ee3f4eaac4994a782d85be2104a35cf5407
1 module( 'mediawiki.Title.js' );
3 // mw.Title relies on these three config vars
4 // Restore them after each test run
5 var _titleConfig = function() {
7         mw.config.set({
8                 "wgFormattedNamespaces": {
9                         "-2": "Media",
10                         "-1": "Special",
11                         "0": "",
12                         "1": "Talk",
13                         "2": "User",
14                         "3": "User talk",
15                         "4": "Wikipedia",
16                         "5": "Wikipedia talk",
17                         "6": "File",
18                         "7": "File talk",
19                         "8": "MediaWiki",
20                         "9": "MediaWiki talk",
21                         "10": "Template",
22                         "11": "Template talk",
23                         "12": "Help",
24                         "13": "Help talk",
25                         "14": "Category",
26                         "15": "Category talk",
27                         /* testing custom / localized */
28                         "100": "Penguins"
29                 },
30                 "wgNamespaceIds": {
31                         "media": -2,
32                         "special": -1,
33                         "": 0,
34                         "talk": 1,
35                         "user": 2,
36                         "user_talk": 3,
37                         "wikipedia": 4,
38                         "wikipedia_talk": 5,
39                         "file": 6,
40                         "file_talk": 7,
41                         "mediawiki": 8,
42                         "mediawiki_talk": 9,
43                         "template": 10,
44                         "template_talk": 11,
45                         "help": 12,
46                         "help_talk": 13,
47                         "category": 14,
48                         "category_talk": 15,
49                         "image": 6,
50                         "image_talk": 7,
51                         "project": 4,
52                         "project_talk": 5,
53                         /* testing custom / alias */
54                         "penguins": 100,
55                         "antarctic_waterfowl": 100
56                 },
57                 "wgCaseSensitiveNamespaces": []
58         });
61 test( '-- Initial check', function() {
62         expect(1);
63         ok( mw.Title, 'mw.Title defined' );
64 });
66 test( 'Transform between Text and Db', function() {
67         expect(2);
68         _titleConfig();
70         var title;
72         title = new mw.Title( 'File:quux pif.jpg' );
73         equal( title.getName(), 'Quux_pif' );
75         title = new mw.Title( 'File:Glarg_foo_glang.jpg' );
76         equal( title.getNameText(), 'Glarg foo glang' );
77 });
79 test( 'Main text for filename', function() {
80         expect(8);
81         _titleConfig();
83         var title = new mw.Title( 'File:foo_bar.JPG' );
85         equal( title.getNamespaceId(), 6 );
86         equal( title.getNamespacePrefix(), 'File:' );
87         equal( title.getName(), 'Foo_bar' );
88         equal( title.getNameText(), 'Foo bar' );
89         equal( title.getMain(), 'Foo_bar.jpg' );
90         equal( title.getMainText(), 'Foo bar.jpg' );
91         equal( title.getExtension(), 'jpg' );
92         equal( title.getDotExtension(), '.jpg' );
93 });
95 test( 'Namespace detection and conversion', function() {
96         expect(6);
97         _titleConfig();
99         var title;
101         title = new mw.Title( 'something.PDF', 6 );
102         equal( title.toString(), 'File:Something.pdf' );
104         title = new mw.Title( 'NeilK', 3 );
105         equal( title.toString(), 'User_talk:NeilK' );
106         equal( title.toText(), 'User talk:NeilK' );
108         title = new mw.Title( 'Frobisher', 100 );
109         equal( title.toString(), 'Penguins:Frobisher' );
111         title = new mw.Title( 'antarctic_waterfowl:flightless_yet_cute.jpg' );
112         equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
114         title = new mw.Title( 'Penguins:flightless_yet_cute.jpg' );
115         equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
118 test( 'Throw error on invalid title', function() {
119         expect(1);
120         _titleConfig();
122         raises(function() {
123                 var title = new mw.Title( '' );
124         }, 'Throw error on empty string' );
127 test( 'Case-sensivity', function() {
128         expect(3);
129         _titleConfig();
131         var title;
133         // Default config
134         mw.config.set( 'wgCaseSensitiveNamespaces', [] );
136         title = new mw.Title( 'article' );
137         equal( title.toString(), 'Article', 'Default config: No sensitive namespaces by default. First-letter becomes uppercase' );
139         // $wgCapitalLinks = false;
140         mw.config.set( 'wgCaseSensitiveNamespaces', [0, -2, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15] );
142         title = new mw.Title( 'article' );
143         equal( title.toString(), 'article', '$wgCapitalLinks=false: Article namespace is sensitive, first-letter case stays lowercase' );
145         title = new mw.Title( 'john', 2 );
146         equal( title.toString(), 'User:John', '$wgCapitalLinks=false: User namespace is insensitive, first-letter becomes uppercase' );
149 test( 'toString / toText', function() {
150         expect(2);
151         _titleConfig();
153         var title = new mw.Title( 'Some random page' );
155         equal( title.toString(), title.getPrefixedDb() );
156         equal( title.toText(), title.getPrefixedText() );
159 test( 'Exists', function() {
160         expect(3);
161         _titleConfig();
163         var title;
165         // Empty registry, checks default to null
167         title = new mw.Title( 'Some random page', 4 );
168         strictEqual( title.exists(), null, 'Return null with empty existance registry' );
170         // Basic registry, checks default to boolean
171         mw.Title.exist.set( ['Does_exist', 'User_talk:NeilK', 'Wikipedia:Sandbox_rules'], true );
172         mw.Title.exist.set( ['Does_not_exist', 'User:John', 'Foobar'], false );
174         title = new mw.Title( 'Project:Sandbox rules' );
175         assertTrue( title.exists(), 'Return true for page titles marked as existing' );
176         title = new mw.Title( 'Foobar' );
177         assertFalse( title.exists(), 'Return false for page titles marked as inexisting' );
181 test( 'Url', function() {
182         expect(2);
183         _titleConfig();
185         var title;
187         // Config
188         mw.config.set( 'wgArticlePath', '/wiki/$1' );
190         title = new mw.Title( 'Foobar' );
191         equal( title.getUrl(), '/wiki/Foobar', 'Basic functionally, toString passing to wikiGetlink' );
193         title = new mw.Title( 'John Doe', 3 );
194         equal( title.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );