~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/tests/filelib_test.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
// You should have received a copy of the GNU General Public License
15
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
 
 
18
17
/**
19
18
 * Unit tests for /lib/filelib.php.
20
19
 *
30
29
require_once($CFG->libdir . '/filelib.php');
31
30
require_once($CFG->dirroot . '/repository/lib.php');
32
31
 
33
 
class filelib_testcase extends advanced_testcase {
 
32
class core_filelib_testcase extends advanced_testcase {
34
33
    public function test_format_postdata_for_curlcall() {
35
34
 
36
 
        //POST params with just simple types
37
 
        $postdatatoconvert =array( 'userid' => 1, 'roleid' => 22, 'name' => 'john');
 
35
        // POST params with just simple types.
 
36
        $postdatatoconvert = array( 'userid' => 1, 'roleid' => 22, 'name' => 'john');
38
37
        $expectedresult = "userid=1&roleid=22&name=john";
39
38
        $postdata = format_postdata_for_curlcall($postdatatoconvert);
40
 
        $this->assertEquals($postdata, $expectedresult);
 
39
        $this->assertEquals($expectedresult, $postdata);
41
40
 
42
 
        //POST params with a string containing & character
43
 
        $postdatatoconvert =array( 'name' => 'john&emilie', 'roleid' => 22);
44
 
        $expectedresult = "name=john%26emilie&roleid=22"; //urlencode: '%26' => '&'
 
41
        // POST params with a string containing & character.
 
42
        $postdatatoconvert = array( 'name' => 'john&emilie', 'roleid' => 22);
 
43
        $expectedresult = "name=john%26emilie&roleid=22"; // Urlencode: '%26' => '&'.
45
44
        $postdata = format_postdata_for_curlcall($postdatatoconvert);
46
 
        $this->assertEquals($postdata, $expectedresult);
 
45
        $this->assertEquals($expectedresult, $postdata);
47
46
 
48
 
        //POST params with an empty value
49
 
        $postdatatoconvert =array( 'name' => null, 'roleid' => 22);
 
47
        // POST params with an empty value.
 
48
        $postdatatoconvert = array( 'name' => null, 'roleid' => 22);
50
49
        $expectedresult = "name=&roleid=22";
51
50
        $postdata = format_postdata_for_curlcall($postdatatoconvert);
52
 
        $this->assertEquals($postdata, $expectedresult);
 
51
        $this->assertEquals($expectedresult, $postdata);
53
52
 
54
 
        //POST params with complex types
55
 
        $postdatatoconvert =array( 'users' => array(
 
53
        // POST params with complex types.
 
54
        $postdatatoconvert = array( 'users' => array(
56
55
            array(
57
56
                'id' => 2,
58
57
                'customfields' => array(
67
66
        );
68
67
        $expectedresult = "users[0][id]=2&users[0][customfields][0][type]=Color&users[0][customfields][0][value]=violet";
69
68
        $postdata = format_postdata_for_curlcall($postdatatoconvert);
70
 
        $this->assertEquals($postdata, $expectedresult);
 
69
        $this->assertEquals($expectedresult, $postdata);
71
70
 
72
 
        //POST params with other complex types
 
71
        // POST params with other complex types.
73
72
        $postdatatoconvert = array ('members' =>
74
73
        array(
75
74
            array('groupid' => 1, 'userid' => 1)
78
77
        );
79
78
        $expectedresult = "members[0][groupid]=1&members[0][userid]=1&members[1][groupid]=1&members[1][userid]=2";
80
79
        $postdata = format_postdata_for_curlcall($postdatatoconvert);
81
 
        $this->assertEquals($postdata, $expectedresult);
 
80
        $this->assertEquals($expectedresult, $postdata);
82
81
    }
83
82
 
84
83
    public function test_download_file_content() {
85
 
        $testhtml = "http://download.moodle.org/unittest/test.html";
86
 
        $contents = download_file_content($testhtml);
87
 
        $this->assertEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
 
84
        global $CFG;
 
85
 
 
86
        // Test http success first.
 
87
        $testhtml = $this->getExternalTestFileUrl('/test.html');
 
88
 
 
89
        $contents = download_file_content($testhtml);
 
90
        $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
 
91
 
 
92
        $tofile = "$CFG->tempdir/test.html";
 
93
        @unlink($tofile);
 
94
        $result = download_file_content($testhtml, null, null, false, 300, 20, false, $tofile);
 
95
        $this->assertTrue($result);
 
96
        $this->assertFileExists($tofile);
 
97
        $this->assertSame(file_get_contents($tofile), $contents);
 
98
        @unlink($tofile);
 
99
 
 
100
        $result = download_file_content($testhtml, null, null, false, 300, 20, false, null, true);
 
101
        $this->assertSame($contents, $result);
 
102
 
 
103
        $response = download_file_content($testhtml, null, null, true);
 
104
        $this->assertInstanceOf('stdClass', $response);
 
105
        $this->assertSame('200', $response->status);
 
106
        $this->assertTrue(is_array($response->headers));
 
107
        $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code));
 
108
        $this->assertSame($contents, $response->results);
 
109
        $this->assertSame('', $response->error);
 
110
 
 
111
        // Test https success.
 
112
        $testhtml = $this->getExternalTestFileUrl('/test.html', true);
 
113
 
 
114
        $contents = download_file_content($testhtml, null, null, false, 300, 20, true);
 
115
        $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
 
116
 
 
117
        $contents = download_file_content($testhtml);
 
118
        $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
 
119
 
 
120
        // Now 404.
 
121
        $testhtml = $this->getExternalTestFileUrl('/test.html_nonexistent');
 
122
 
 
123
        $contents = download_file_content($testhtml);
 
124
        $this->assertFalse($contents);
 
125
        $this->assertDebuggingCalled();
 
126
 
 
127
        $response = download_file_content($testhtml, null, null, true);
 
128
        $this->assertInstanceOf('stdClass', $response);
 
129
        $this->assertSame('404', $response->status);
 
130
        $this->assertTrue(is_array($response->headers));
 
131
        $this->assertRegExp('|^HTTP/1\.[01] 404 Not Found$|', rtrim($response->response_code));
 
132
        // Do not test the response starts with DOCTYPE here because some servers may return different headers.
 
133
        $this->assertSame('', $response->error);
 
134
 
 
135
        // Invalid url.
 
136
        $testhtml = $this->getExternalTestFileUrl('/test.html');
 
137
        $testhtml = str_replace('http://', 'ftp://', $testhtml);
 
138
 
 
139
        $contents = download_file_content($testhtml);
 
140
        $this->assertFalse($contents);
 
141
 
 
142
        // Test standard redirects.
 
143
        $testurl = $this->getExternalTestFileUrl('/test_redir.php');
 
144
 
 
145
        $contents = download_file_content("$testurl?redir=2");
 
146
        $this->assertSame('done', $contents);
 
147
 
 
148
        $response = download_file_content("$testurl?redir=2", null, null, true);
 
149
        $this->assertInstanceOf('stdClass', $response);
 
150
        $this->assertSame('200', $response->status);
 
151
        $this->assertTrue(is_array($response->headers));
 
152
        $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code));
 
153
        $this->assertSame('done', $response->results);
 
154
        $this->assertSame('', $response->error);
 
155
 
 
156
        // Commented out this block if there are performance problems.
 
157
        /*
 
158
        $contents = download_file_content("$testurl?redir=6");
 
159
        $this->assertFalse(false, $contents);
 
160
        $this->assertDebuggingCalled();
 
161
        $response = download_file_content("$testurl?redir=6", null, null, true);
 
162
        $this->assertInstanceOf('stdClass', $response);
 
163
        $this->assertSame('0', $response->status);
 
164
        $this->assertTrue(is_array($response->headers));
 
165
        $this->assertFalse($response->results);
 
166
        $this->assertNotEmpty($response->error);
 
167
        */
 
168
 
 
169
        // Test relative redirects.
 
170
        $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
 
171
 
 
172
        $contents = download_file_content("$testurl");
 
173
        $this->assertSame('done', $contents);
 
174
 
 
175
        $contents = download_file_content("$testurl?unused=xxx");
 
176
        $this->assertSame('done', $contents);
 
177
    }
 
178
 
 
179
    /**
 
180
     * Test curl basics.
 
181
     */
 
182
    public function test_curl_basics() {
 
183
        global $CFG;
 
184
 
 
185
        // Test HTTP success.
 
186
        $testhtml = $this->getExternalTestFileUrl('/test.html');
 
187
 
 
188
        $curl = new curl();
 
189
        $contents = $curl->get($testhtml);
 
190
        $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
 
191
        $this->assertSame(0, $curl->get_errno());
 
192
 
 
193
        $curl = new curl();
 
194
        $tofile = "$CFG->tempdir/test.html";
 
195
        @unlink($tofile);
 
196
        $fp = fopen($tofile, 'w');
 
197
        $result = $curl->get($testhtml, array(), array('CURLOPT_FILE'=>$fp));
 
198
        $this->assertTrue($result);
 
199
        fclose($fp);
 
200
        $this->assertFileExists($tofile);
 
201
        $this->assertSame($contents, file_get_contents($tofile));
 
202
        @unlink($tofile);
 
203
 
 
204
        $curl = new curl();
 
205
        $tofile = "$CFG->tempdir/test.html";
 
206
        @unlink($tofile);
 
207
        $result = $curl->download_one($testhtml, array(), array('filepath'=>$tofile));
 
208
        $this->assertTrue($result);
 
209
        $this->assertFileExists($tofile);
 
210
        $this->assertSame($contents, file_get_contents($tofile));
 
211
        @unlink($tofile);
 
212
 
 
213
        // Test 404 request.
 
214
        $curl = new curl();
 
215
        $contents = $curl->get($this->getExternalTestFileUrl('/i.do.not.exist'));
 
216
        $response = $curl->getResponse();
 
217
        $this->assertSame('404 Not Found', reset($response));
 
218
        $this->assertSame(0, $curl->get_errno());
 
219
    }
 
220
 
 
221
    public function test_curl_redirects() {
 
222
        global $CFG;
 
223
 
 
224
        // Test full URL redirects.
 
225
        $testurl = $this->getExternalTestFileUrl('/test_redir.php');
 
226
 
 
227
        $curl = new curl();
 
228
        $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
 
229
        $response = $curl->getResponse();
 
230
        $this->assertSame('200 OK', reset($response));
 
231
        $this->assertSame(0, $curl->get_errno());
 
232
        $this->assertSame(2, $curl->info['redirect_count']);
 
233
        $this->assertSame('done', $contents);
 
234
 
 
235
        $curl = new curl();
 
236
        $curl->emulateredirects = true;
 
237
        $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
 
238
        $response = $curl->getResponse();
 
239
        $this->assertSame('200 OK', reset($response));
 
240
        $this->assertSame(0, $curl->get_errno());
 
241
        $this->assertSame(2, $curl->info['redirect_count']);
 
242
        $this->assertSame('done', $contents);
 
243
 
 
244
        $curl = new curl();
 
245
        $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
 
246
        $response = $curl->getResponse();
 
247
        $this->assertSame('302 Found', reset($response));
 
248
        $this->assertSame(0, $curl->get_errno());
 
249
        $this->assertSame(302, $curl->info['http_code']);
 
250
        $this->assertSame('', $contents);
 
251
 
 
252
        $curl = new curl();
 
253
        $curl->emulateredirects = true;
 
254
        $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
 
255
        $response = $curl->getResponse();
 
256
        $this->assertSame('302 Found', reset($response));
 
257
        $this->assertSame(0, $curl->get_errno());
 
258
        $this->assertSame(302, $curl->info['http_code']);
 
259
        $this->assertSame('', $contents);
 
260
 
 
261
        $curl = new curl();
 
262
        $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
 
263
        $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
 
264
        $this->assertNotEmpty($contents);
 
265
 
 
266
        $curl = new curl();
 
267
        $curl->emulateredirects = true;
 
268
        $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
 
269
        $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
 
270
        $this->assertNotEmpty($contents);
 
271
 
 
272
        $curl = new curl();
 
273
        $tofile = "$CFG->tempdir/test.html";
 
274
        @unlink($tofile);
 
275
        $fp = fopen($tofile, 'w');
 
276
        $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
 
277
        $this->assertTrue($result);
 
278
        fclose($fp);
 
279
        $this->assertFileExists($tofile);
 
280
        $this->assertSame('done', file_get_contents($tofile));
 
281
        @unlink($tofile);
 
282
 
 
283
        $curl = new curl();
 
284
        $curl->emulateredirects = true;
 
285
        $tofile = "$CFG->tempdir/test.html";
 
286
        @unlink($tofile);
 
287
        $fp = fopen($tofile, 'w');
 
288
        $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
 
289
        $this->assertTrue($result);
 
290
        fclose($fp);
 
291
        $this->assertFileExists($tofile);
 
292
        $this->assertSame('done', file_get_contents($tofile));
 
293
        @unlink($tofile);
 
294
 
 
295
        $curl = new curl();
 
296
        $tofile = "$CFG->tempdir/test.html";
 
297
        @unlink($tofile);
 
298
        $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
 
299
        $this->assertTrue($result);
 
300
        $this->assertFileExists($tofile);
 
301
        $this->assertSame('done', file_get_contents($tofile));
 
302
        @unlink($tofile);
 
303
 
 
304
        $curl = new curl();
 
305
        $curl->emulateredirects = true;
 
306
        $tofile = "$CFG->tempdir/test.html";
 
307
        @unlink($tofile);
 
308
        $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
 
309
        $this->assertTrue($result);
 
310
        $this->assertFileExists($tofile);
 
311
        $this->assertSame('done', file_get_contents($tofile));
 
312
        @unlink($tofile);
 
313
    }
 
314
 
 
315
    public function test_curl_relative_redirects() {
 
316
        // Test relative location redirects.
 
317
        $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
 
318
 
 
319
        $curl = new curl();
 
320
        $contents = $curl->get($testurl);
 
321
        $response = $curl->getResponse();
 
322
        $this->assertSame('200 OK', reset($response));
 
323
        $this->assertSame(0, $curl->get_errno());
 
324
        $this->assertSame(1, $curl->info['redirect_count']);
 
325
        $this->assertSame('done', $contents);
 
326
 
 
327
        $curl = new curl();
 
328
        $curl->emulateredirects = true;
 
329
        $contents = $curl->get($testurl);
 
330
        $response = $curl->getResponse();
 
331
        $this->assertSame('200 OK', reset($response));
 
332
        $this->assertSame(0, $curl->get_errno());
 
333
        $this->assertSame(1, $curl->info['redirect_count']);
 
334
        $this->assertSame('done', $contents);
 
335
 
 
336
        // Test different redirect types.
 
337
        $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
 
338
 
 
339
        $curl = new curl();
 
340
        $contents = $curl->get("$testurl?type=301");
 
341
        $response = $curl->getResponse();
 
342
        $this->assertSame('200 OK', reset($response));
 
343
        $this->assertSame(0, $curl->get_errno());
 
344
        $this->assertSame(1, $curl->info['redirect_count']);
 
345
        $this->assertSame('done', $contents);
 
346
 
 
347
        $curl = new curl();
 
348
        $curl->emulateredirects = true;
 
349
        $contents = $curl->get("$testurl?type=301");
 
350
        $response = $curl->getResponse();
 
351
        $this->assertSame('200 OK', reset($response));
 
352
        $this->assertSame(0, $curl->get_errno());
 
353
        $this->assertSame(1, $curl->info['redirect_count']);
 
354
        $this->assertSame('done', $contents);
 
355
 
 
356
        $curl = new curl();
 
357
        $contents = $curl->get("$testurl?type=302");
 
358
        $response = $curl->getResponse();
 
359
        $this->assertSame('200 OK', reset($response));
 
360
        $this->assertSame(0, $curl->get_errno());
 
361
        $this->assertSame(1, $curl->info['redirect_count']);
 
362
        $this->assertSame('done', $contents);
 
363
 
 
364
        $curl = new curl();
 
365
        $curl->emulateredirects = true;
 
366
        $contents = $curl->get("$testurl?type=302");
 
367
        $response = $curl->getResponse();
 
368
        $this->assertSame('200 OK', reset($response));
 
369
        $this->assertSame(0, $curl->get_errno());
 
370
        $this->assertSame(1, $curl->info['redirect_count']);
 
371
        $this->assertSame('done', $contents);
 
372
 
 
373
        $curl = new curl();
 
374
        $contents = $curl->get("$testurl?type=303");
 
375
        $response = $curl->getResponse();
 
376
        $this->assertSame('200 OK', reset($response));
 
377
        $this->assertSame(0, $curl->get_errno());
 
378
        $this->assertSame(1, $curl->info['redirect_count']);
 
379
        $this->assertSame('done', $contents);
 
380
 
 
381
        $curl = new curl();
 
382
        $curl->emulateredirects = true;
 
383
        $contents = $curl->get("$testurl?type=303");
 
384
        $response = $curl->getResponse();
 
385
        $this->assertSame('200 OK', reset($response));
 
386
        $this->assertSame(0, $curl->get_errno());
 
387
        $this->assertSame(1, $curl->info['redirect_count']);
 
388
        $this->assertSame('done', $contents);
 
389
 
 
390
        $curl = new curl();
 
391
        $contents = $curl->get("$testurl?type=307");
 
392
        $response = $curl->getResponse();
 
393
        $this->assertSame('200 OK', reset($response));
 
394
        $this->assertSame(0, $curl->get_errno());
 
395
        $this->assertSame(1, $curl->info['redirect_count']);
 
396
        $this->assertSame('done', $contents);
 
397
 
 
398
        $curl = new curl();
 
399
        $curl->emulateredirects = true;
 
400
        $contents = $curl->get("$testurl?type=307");
 
401
        $response = $curl->getResponse();
 
402
        $this->assertSame('200 OK', reset($response));
 
403
        $this->assertSame(0, $curl->get_errno());
 
404
        $this->assertSame(1, $curl->info['redirect_count']);
 
405
        $this->assertSame('done', $contents);
 
406
 
 
407
        $curl = new curl();
 
408
        $contents = $curl->get("$testurl?type=308");
 
409
        $response = $curl->getResponse();
 
410
        $this->assertSame('200 OK', reset($response));
 
411
        $this->assertSame(0, $curl->get_errno());
 
412
        $this->assertSame(1, $curl->info['redirect_count']);
 
413
        $this->assertSame('done', $contents);
 
414
 
 
415
        $curl = new curl();
 
416
        $curl->emulateredirects = true;
 
417
        $contents = $curl->get("$testurl?type=308");
 
418
        $response = $curl->getResponse();
 
419
        $this->assertSame('200 OK', reset($response));
 
420
        $this->assertSame(0, $curl->get_errno());
 
421
        $this->assertSame(1, $curl->info['redirect_count']);
 
422
        $this->assertSame('done', $contents);
 
423
 
 
424
    }
 
425
 
 
426
    public function test_curl_proxybypass() {
 
427
        global $CFG;
 
428
        $testurl = $this->getExternalTestFileUrl('/test.html');
 
429
 
 
430
        $oldproxy = $CFG->proxyhost;
 
431
        $oldproxybypass = $CFG->proxybypass;
 
432
 
 
433
        // Test without proxy bypass and inaccessible proxy.
 
434
        $CFG->proxyhost = 'i.do.not.exist';
 
435
        $CFG->proxybypass = '';
 
436
        $curl = new curl();
 
437
        $contents = $curl->get($testurl);
 
438
        $this->assertNotEquals(0, $curl->get_errno());
 
439
        $this->assertNotEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
 
440
 
 
441
        // Test with proxy bypass.
 
442
        $testurlhost = parse_url($testurl, PHP_URL_HOST);
 
443
        $CFG->proxybypass = $testurlhost;
 
444
        $curl = new curl();
 
445
        $contents = $curl->get($testurl);
 
446
        $this->assertSame(0, $curl->get_errno());
 
447
        $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
 
448
 
 
449
        $CFG->proxyhost = $oldproxy;
 
450
        $CFG->proxybypass = $oldproxybypass;
 
451
    }
 
452
 
 
453
    public function test_curl_post() {
 
454
        $testurl = $this->getExternalTestFileUrl('/test_post.php');
 
455
 
 
456
        // Test post request.
 
457
        $curl = new curl();
 
458
        $contents = $curl->post($testurl, 'data=moodletest');
 
459
        $response = $curl->getResponse();
 
460
        $this->assertSame('200 OK', reset($response));
 
461
        $this->assertSame(0, $curl->get_errno());
 
462
        $this->assertSame('OK', $contents);
 
463
 
 
464
        // Test 100 requests.
 
465
        $curl = new curl();
 
466
        $curl->setHeader('Expect: 100-continue');
 
467
        $contents = $curl->post($testurl, 'data=moodletest');
 
468
        $response = $curl->getResponse();
 
469
        $this->assertSame('200 OK', reset($response));
 
470
        $this->assertSame(0, $curl->get_errno());
 
471
        $this->assertSame('OK', $contents);
 
472
    }
 
473
 
 
474
    public function test_curl_file() {
 
475
        $this->resetAfterTest();
 
476
        $testurl = $this->getExternalTestFileUrl('/test_file.php');
 
477
 
 
478
        $fs = get_file_storage();
 
479
        $filerecord = array(
 
480
            'contextid' => context_system::instance()->id,
 
481
            'component' => 'test',
 
482
            'filearea' => 'curl_post',
 
483
            'itemid' => 0,
 
484
            'filepath' => '/',
 
485
            'filename' => 'test.txt'
 
486
        );
 
487
        $teststring = 'moodletest';
 
488
        $testfile = $fs->create_file_from_string($filerecord, $teststring);
 
489
 
 
490
        // Test post with file.
 
491
        $data = array('testfile' => $testfile);
 
492
        $curl = new curl();
 
493
        $contents = $curl->post($testurl, $data);
 
494
        $this->assertSame('OK', $contents);
88
495
    }
89
496
 
90
497
    /**
135
542
        $fileid = $originalfile->get_id();
136
543
        $this->assertInstanceOf('stored_file', $originalfile);
137
544
 
138
 
        // create a user private file
 
545
        // Create a user private file.
139
546
        $userfilerecord = new stdClass;
140
547
        $userfilerecord->contextid = $usercontext->id;
141
548
        $userfilerecord->component = 'user';
149
556
 
150
557
        $filerefrecord = clone((object)$filerecord);
151
558
        $filerefrecord->filename = 'testref.txt';
152
 
        // create a file reference
 
559
 
 
560
        // Create a file reference.
153
561
        $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
154
562
        $this->assertInstanceOf('stored_file', $fileref);
155
563
        $this->assertEquals($userrepository->id, $fileref->get_repository_id());
156
 
        $this->assertEquals($userfile->get_contenthash(), $fileref->get_contenthash());
 
564
        $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
157
565
        $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
158
566
        $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
159
567
 
161
569
        file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
162
570
 
163
571
        $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
164
 
        $this->assertEquals(3, count($draftfiles));
 
572
        $this->assertCount(3, $draftfiles);
165
573
 
166
574
        $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
167
575
        $source = unserialize($draftfile->get_source());
168
 
        $this->assertEquals($ref, $source->original);
169
 
        $this->assertEquals($sourcefield, $source->source);
 
576
        $this->assertSame($ref, $source->original);
 
577
        $this->assertSame($sourcefield, $source->source);
170
578
 
171
579
        $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
172
580
        $this->assertInstanceOf('stored_file', $draftfileref);
173
 
        $this->assertEquals(true, $draftfileref->is_external_file());
 
581
        $this->assertTrue($draftfileref->is_external_file());
174
582
 
175
 
        // change some information
 
583
        // Change some information.
176
584
        $author = 'Dongsheng Cai';
177
585
        $draftfile->set_author($author);
178
586
        $newsourcefield = 'Get from Flickr';
179
587
        $license = 'GPLv3';
180
588
        $draftfile->set_license($license);
181
 
        // if you want to really just change source field, do this:
 
589
        // If you want to really just change source field, do this.
182
590
        $source = unserialize($draftfile->get_source());
183
591
        $newsourcefield = 'From flickr';
184
592
        $source->source = $newsourcefield;
185
593
        $draftfile->set_source(serialize($source));
186
594
 
187
 
        // Save changed file
 
595
        // Save changed file.
188
596
        file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
189
597
 
190
598
        $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
191
599
 
192
 
        // Make sure it's the original file id
 
600
        // Make sure it's the original file id.
193
601
        $this->assertEquals($fileid, $file->get_id());
194
602
        $this->assertInstanceOf('stored_file', $file);
195
 
        $this->assertEquals($author, $file->get_author());
196
 
        $this->assertEquals($license, $file->get_license());
 
603
        $this->assertSame($author, $file->get_author());
 
604
        $this->assertSame($license, $file->get_license());
197
605
        $this->assertEquals($newsourcefield, $file->get_source());
198
606
    }
199
607
 
200
608
    /**
201
 
     * Testing deleting original files
 
609
     * Testing deleting original files.
202
610
     *
203
611
     * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
204
612
     * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
226
634
 
227
635
        $filecontent = 'User file content';
228
636
 
229
 
        // create a user private file
 
637
        // Create a user private file.
230
638
        $userfilerecord = new stdClass;
231
639
        $userfilerecord->contextid = $usercontext->id;
232
640
        $userfilerecord->component = 'user';
247
655
            'filepath'  => '/',
248
656
            'filename'  => 'test.txt',
249
657
        );
250
 
        // create a file reference
 
658
        // Create a file reference.
251
659
        $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
252
660
        $this->assertInstanceOf('stored_file', $fileref);
253
661
        $this->assertEquals($userrepository->id, $fileref->get_repository_id());
254
 
        $this->assertEquals($userfile->get_contenthash(), $fileref->get_contenthash());
 
662
        $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
255
663
        $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
256
664
        $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
257
665
 
258
666
        $draftitemid = 0;
259
667
        file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0);
260
668
        $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
261
 
        $this->assertEquals(2, count($draftfiles));
 
669
        $this->assertCount(2, $draftfiles);
262
670
        $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
263
671
        $draftfile->delete();
264
 
        // Save changed file
 
672
        // Save changed file.
265
673
        file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
266
674
 
267
 
        // The file reference should be a regular moodle file now
 
675
        // The file reference should be a regular moodle file now.
268
676
        $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
269
 
        $this->assertEquals(false, $fileref->is_external_file());
270
 
        $this->assertEquals($contenthash, $fileref->get_contenthash());
 
677
        $this->assertFalse($fileref->is_external_file());
 
678
        $this->assertSame($contenthash, $fileref->get_contenthash());
271
679
        $this->assertEquals($filecontent, $fileref->get_content());
272
680
    }
273
681
 
325
733
        $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected);
326
734
 
327
735
        // Test stripping works OK.
328
 
        $this->assertEquals($mdl30648expected, curl::strip_double_headers($mdl30648example));
 
736
        $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example));
329
737
        // Test it does nothing to the 'plain' data.
330
 
        $this->assertEquals($mdl30648expected, curl::strip_double_headers($mdl30648expected));
 
738
        $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected));
331
739
 
332
740
        // Example from OU proxy.
333
741
        $httpsexample = <<<EOF
363
771
        $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
364
772
 
365
773
        // Test stripping works OK.
366
 
        $this->assertEquals($httpsexpected, curl::strip_double_headers($httpsexample));
 
774
        $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
367
775
        // Test it does nothing to the 'plain' data.
368
 
        $this->assertEquals($httpsexpected, curl::strip_double_headers($httpsexpected));
 
776
        $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
369
777
    }
370
778
}