Debian Codegraph @

tests/qunit/resources/mediawiki.storage.test.js

raw symbols SCIP
1 ( function () {
2 const EXPIRY_PREFIX = '_EXPIRY_';
3
4 QUnit.module( 'mediawiki.storage' );
5
6 QUnit.test( 'set/get(Object) with storage support', function ( assert ) {
7 const data = {},
8 done = assert.async(),
9 object = { test: 'value' },
10 stub = {
11 setItem: function ( k, v ) {
12 data[ k ] = v;
13 return true;
14 },
15 getItem: function ( k ) {
16 return Object.prototype.hasOwnProperty.call( data, k ) ? data[ k ] : null;
17 },
18 removeItem: function ( k ) {
19 delete data[ k ];
20 return true;
21 },
22 key: function ( i ) {
23 return Object.keys( data )[ i ];
24 }
25 };
26
27 Object.defineProperty( stub, 'length', {
28 get: function () {
29 return Object.keys( data ).length;
30 }
31 } );
32
33 this.sandbox.stub( mw.storage, 'store', stub );
34
35 assert.strictEqual( mw.storage.set( 'foo', 'test' ), true, 'set returns true' );
36 assert.strictEqual( mw.storage.get( 'foo' ), 'test', 'Check value gets stored' );
37 assert.strictEqual( mw.storage.get( 'bar' ), null, 'Unset values are null' );
38 assert.strictEqual( mw.storage.remove( 'foo' ), true, 'remove returns true' );
39 assert.strictEqual( mw.storage.get( 'foo' ), null, 'Removed item is null' );
40
41 assert.strictEqual( mw.storage.setObject( 'baz', object ), true, 'setObject returns true' );
42 assert.deepEqual( mw.storage.getObject( 'baz' ), object, 'Check value gets stored' );
43 assert.notStrictEqual( mw.storage.getObject( 'baz' ), object, 'Retrieved value is a new object' );
44 assert.strictEqual( mw.storage.getObject( 'quux' ), null, 'Unset values are null' );
45 assert.strictEqual( mw.storage.remove( 'baz' ), true, 'remove returns true' );
46 assert.strictEqual( mw.storage.getObject( 'baz' ), null, 'Removed item is null' );
47
48 mw.storage.set( 'baz', 'Non-JSON' );
49 assert.strictEqual( mw.storage.getObject( 'baz' ), null, 'Non-JSON values are null' );
50
51 const now = Math.floor( Date.now() / 1000 );
52 mw.storage.set( 'foo', 'test', 60 * 60 );
53 assert.true( mw.storage.get( EXPIRY_PREFIX + 'foo' ) > now, 'Future expiry time stored' );
54 assert.strictEqual( mw.storage.get( 'foo' ), 'test', 'Non-expired item fetched from store' );
55
56 mw.storage.setObject( 'foo', 'test', 60 * 60 );
57 assert.true( mw.storage.get( EXPIRY_PREFIX + 'foo' ) > now, 'Future expiry time stored (object)' );
58 assert.strictEqual( mw.storage.getObject( 'foo' ), 'test', 'Non-expired item fetched from store (object)' );
59
60 mw.storage.setObject( 'foo', 'test', -60 );
61 assert.strictEqual( mw.storage.get( 'foo' ), null, 'Expired item returns null' );
62 assert.strictEqual( data.foo, '"test"', 'Expired item exists in storage' );
63
64 mw.storage.set( 'baz', 'test' );
65 assert.strictEqual( mw.storage.get( EXPIRY_PREFIX + 'baz' ), null, 'Item with no expiry has no expiry item' );
66
67 mw.storage.set( 'foo', 'test', 60 * 60 );
68 mw.storage.remove( 'foo' );
69 assert.strictEqual( mw.storage.get( EXPIRY_PREFIX + 'baz' ), null, 'Removed item has no expiry' );
70
71 assert.throws( () => {
72 mw.storage.set( EXPIRY_PREFIX + 'foo', 'test', 60 * 60 );
73 }, 'Error thrown when key prefix conflicts with EXPIRY_PREFIX' );
74
75 mw.storage.clearExpired().then( () => {
76 assert.deepEqual( Object.keys( data ), [ 'baz' ], 'Only unexpired keys present after #clearExpired' );
77 done();
78 } );
79 } );
80
81 QUnit.test( 'set/get(Object) with storage methods disabled', function ( assert ) {
82 // This covers browsers where storage is disabled
83 // (quota full, or security/privacy settings).
84 // On most browsers, these interface will be accessible with
85 // their methods throwing.
86 const stub = {
87 getItem: this.sandbox.stub(),
88 removeItem: this.sandbox.stub(),
89 setItem: this.sandbox.stub()
90 };
91 stub.getItem.throws();
92 stub.setItem.throws();
93 stub.removeItem.throws();
94 this.sandbox.stub( mw.storage, 'store', stub );
95
96 assert.strictEqual( mw.storage.get( 'foo' ), false );
97 assert.strictEqual( mw.storage.set( 'foo', 'test' ), false );
98 assert.strictEqual( mw.storage.remove( 'foo' ), false );
99
100 assert.strictEqual( mw.storage.getObject( 'bar' ), false );
101 assert.strictEqual( mw.storage.setObject( 'bar', { test: 'value' } ), false );
102 assert.strictEqual( mw.storage.remove( 'bar' ), false );
103 } );
104
105 QUnit.test( 'set/get(Object) with storage object disabled', ( assert ) => {
106 // On other browsers, these entire object is disabled.
107 // `'localStorage' in window` would be true (and pass feature test)
108 // but trying to read the object as window.localStorage would throw
109 // an exception. Such case would instantiate SafeStorage with
110 // undefined after the internal try/catch.
111 const old = mw.storage.store;
112 mw.storage.store = undefined;
113
114 assert.strictEqual( mw.storage.get( 'foo' ), false );
115 assert.strictEqual( mw.storage.set( 'foo', 'test' ), false );
116 assert.strictEqual( mw.storage.remove( 'foo', 'test' ), false );
117
118 assert.strictEqual( mw.storage.getObject( 'bar' ), false );
119 assert.strictEqual( mw.storage.setObject( 'bar', { test: 'value' } ), false );
120 assert.strictEqual( mw.storage.remove( 'bar' ), false );
121
122 mw.storage.store = old;
123 } );
124
125 QUnit.test( 'set/get with expiry - partial failure', function ( assert ) {
126 const store = {};
127 const stub = {
128 setItem: this.sandbox.spy( ( k, v ) => {
129 if ( k.startsWith( EXPIRY_PREFIX ) ) {
130 // Mock a failing store when trying to set a key with expiry
131 throw new Error();
132 }
133 store[ k ] = v;
134 } ),
135 getItem: function ( k ) {
136 return store[ k ] || null;
137 },
138 removeItem: function ( k ) {
139 delete store[ k ];
140 }
141 };
142 this.sandbox.stub( mw.storage, 'store', stub );
143
144 // Test the test code above
145 mw.storage.set( 'bar', 'test' );
146 assert.strictEqual( mw.storage.get( 'bar' ), 'test' );
147
148 // Test the failure behavior when setting expiry
149 mw.storage.set( 'foo', 'test', 99 );
150 assert.strictEqual( stub.setItem.withArgs( EXPIRY_PREFIX + 'foo' ).calledOnce, true );
151 assert.strictEqual( stub.setItem.withArgs( 'foo' ).notCalled, true );
152 assert.strictEqual( mw.storage.get( 'foo' ), null, 'Item was not set when setting expiry failed' );
153
154 } );
155
156 }() );