-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproject_src_github.test
More file actions
583 lines (500 loc) · 23.2 KB
/
Copy pathproject_src_github.test
File metadata and controls
583 lines (500 loc) · 23.2 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<?php
/**
* @file
* SimpleTest tests for the Project Source: GitHub module.
*/
/**
* Helper class with some added functions and properties for testing.
*/
class ProjectSrcGithubBaseCase extends DrupalWebTestCase {
/**
* The GitHub organization used for testing.
*/
protected $org = 'drewpaul_inc';
/**
* @var GithubApiMock
*/
protected $github;
function setUp(array $modules = array()) {
$modules[] = 'project_src';
$modules[] = 'project_src_github';
parent::setUp($modules);
// Ensure the test environment has the proper public files directory.
variable_del('file_public_path');
// Ensure the test environment has clean URLs enabled.
variable_set('clean_url', TRUE);
// Order matters!
$fake_api = 'project_src_github_test_github_api';
$this->fakeModuleUninstall('composer_manager');
$this->fakeModuleUninstall('github_api');
drupal_flush_all_caches();
$this->fakeModuleInstall($fake_api);
// Set up the mock paginator.
variable_set('project_src_github_paginator_class', 'GithubPaginatorMock');
// Set relevant Project Source: GitHub variables.
variable_set('project_src_github_org', $this->org);
$module = drupal_get_path('module', $fake_api);
$github = $this->getAbsoluteUrl($module . '/archives');
variable_set('project_src_github_domain', $github);
// Keep track of our fake GitHub API object for test comparisons.
module_load_include('module', $fake_api, $fake_api);
$this->github = new GithubApiMock();
}
/**
* Performs an unofficial module install (in that no hooks are run and no
* dependencies are loaded or installed).
*
* @param string $module
* The module to "install."
*/
protected function fakeModuleInstall($module) {
db_update('system')
->fields(array('status' => 1))
->condition('type', 'module')
->condition('name', $module)
->execute();
system_list_reset();
module_list(TRUE);
module_implements('', FALSE, TRUE);
_system_update_bootstrap_status();
registry_update();
drupal_get_schema(NULL, TRUE);
drupal_theme_rebuild();
entity_info_cache_clear();
}
/**
* Performs an unofficial module uninstall (in that no hooks are run and no
* dependencies are loaded or installed).
*
* @param string $module
* The module to "uninstall."
*/
protected function fakeModuleUninstall($module) {
db_update('system')
->fields(array('status' => 0))
->condition('type', 'module')
->condition('name', $module)
->execute();
system_list_reset();
module_list(TRUE);
module_implements('', FALSE, TRUE);
entity_info_cache_clear();
registry_update();
_system_update_bootstrap_status();
drupal_theme_rebuild();
}
}
/**
* Tests Project Source: GitHub XML generation.
*/
class ProjectSrcGithubXMLTests extends ProjectSrcGithubBaseCase {
public static function getInfo() {
return array(
'name' => 'Project Source GitHub - XML',
'description' => 'Tests the Project Source GitHub XML generation.',
'group' => 'Project Source GitHub',
);
}
/**
* Compares the provided project XML values with those provided by our mock
* GitHub API object.
*
* @param SimpleXMLElement|array $xml
* The SimpleXMLElement as returned by the xpath query (or an array that
* contains at a SimpleXMLElement as its first item).
* @param string $api
* The API version of the requested project XML.
*/
protected function validateProjectXml($xml, $api) {
$repos = $this->github->repositories($this->org);
$repo = $repos[$this->org . '_module'];
// We may be handed an array.
if (is_array($xml)) {
$xml = $xml[0];
}
$major_version = substr($repo['default_branch'], 4, 1);
$this->assertEqual($xml->title, $repo['name'], format_string('Returned the expected project title %arg.', array('%arg' => $xml->title)));
$this->assertEqual($xml->short_name, $repo['name'], format_string('Returned the expected project short name %arg.', array('%arg' => $xml->short_name)));
$this->assertEqual($xml->creator, $repo['owner']['login'], format_string('Returned the expected project creator %arg.', array('%arg' => $xml->creator)));
$this->assertEqual($xml->api_version, $api, format_string('Returned the expected API version %arg.', array('%arg' => $xml->api_version)));
$this->assertEqual($xml->default_major, $major_version, format_string('Returned the expected default major version %arg.', array('%arg' => $xml->default_major)));
$this->assertEqual($xml->project_status, 'published', 'Returned the expected status.');
// @todo Recommended/supported major?
// @todo Link?
}
/**
* Compares the provided release XML values with those provided by our mock
* GitHub API object.
*
* @param SimpleXMLElement $xml
* The SimpleXMLElement as returned by the xpath query.
*/
protected function validateReleaseXml($xml) {
$github = variable_get('project_src_github_domain', 'https://github.com');
$repos = $this->github->repositories($this->org);
$repo = $repos[$this->org . '_module'];
$branches = $this->github->branches($this->org, $this->org . '_module');
$commit = $this->github->show($this->org, $this->org . '_module', '');
// Tests if we're dealing with a dev release.
if (substr($xml->version, -4) == '-dev') {
$expected_extra = 'dev';
$real_tag = substr($xml->version, 0, -4);
$release_link = $github . '/' . $repo['full_name'] . '/tree/' . $real_tag;
}
// Test if we're dealing with an unstable release.
elseif (preg_match_all('/.*?((unstable|alpha|beta|rc)\\d+)/is', $xml->version, $matches)) {
$expected_extra = $matches[1][0];
$real_tag = $xml->version;
$release_link = $github . '/' . $repo['full_name'] . '/releases/' . $real_tag;
}
else {
$expected_extra = NULL;
$real_tag = $xml->version;
$release_link = $github . '/' . $repo['full_name'] . '/releases/' . $real_tag;
}
$name = $this->org . '_module ' . $xml->version;
$major_version = substr($real_tag, 4, 1);
$file_name = 'project-src-github/' . $repo['full_name'] . '/' . $real_tag . '/' . 'download';
$this->assertEqual($xml->name, $name, format_string('Returned the expected release name %arg.', array('%arg' => $xml->name)));
$this->assertEqual($xml->version, $xml->tag, format_string('Returned the expected release tag %arg.', array('%arg' => $xml->tag)));
$this->assertEqual($xml->version_major, $major_version, format_string('Returned the expected major version %arg.', array('%arg' => $xml->version_major)));
$this->assertTrue(strpos($xml->download_link, $file_name) !== FALSE, format_string('Returned the expected release download link %arg.', array('%arg' => $xml->download_link)));
$this->assertEqual($xml->date, strtotime($commit['commit']['author']['date']), format_string('Returned the expected release date %arg.', array('%arg' => $xml->date)));
$this->assertEqual($xml->release_link, $release_link, format_string('Returned the expected release link %arg.', array('%arg' => $xml->release_link)));
$this->assertEqual($xml->status, 'published', 'Returned valid status.');
// If this release corresponds to a dev/alpha/beta/etc. release, test it.
if (!empty($expected_extra)) {
$this->assertEqual($xml->version_extra, $expected_extra, format_string('Returned expected extra version metadata %arg.', array('%arg' => $xml->version_extra)));
// Ensure that dev tags correspond to actual branches from the API.
if ($expected_extra == 'dev') {
$this->assertTrue(isset($branches[$real_tag]), format_string('Release %arg matches a valid branch.', array('%arg' => $real_tag)));
}
}
}
/**
* Compares the provided file XML values with those provided by our mock
* GitHub API object.
*
* @param SimpleXMLElement $xml
* The SimpleXMLElement as returned by the xpath query.
*/
protected function validateFileXml($xml) {
$repos = $this->github->repositories($this->org);
$repo = $repos[$this->org . '_module'];
$commit = $this->github->show($this->org, $this->org . '_module', '');
$version = '';
if (preg_match_all('/.*?(\\d+\\.x-\\d+\\..(-.*\\d+)?).*?/is', $xml->url, $matches)) {
$version = $matches[1][0];
}
elseif (preg_match_all('', $xml->url, $matches)) {
}
$file_name = 'project-src-github/' . $repo['full_name'] . '/' . $version . '/' . 'download';
// Note File size and md5 are validated separately.
$this->assertTrue(strpos($xml->url, $file_name) !== FALSE, format_string('Returned the expected release download link %arg.', array('%arg' => $xml->url)));
$this->assertEqual($xml->archive_type, 'tar.gz', 'Returned the expected archive type.');
$this->assertEqual($xml->filedate, strtotime($commit['commit']['author']['date']), format_string('Returned the expected file date.', array('%arg' => $xml->filedate)));
}
/**
* Main XML generation test method.
*/
public function testProjectSourceProjectXml() {
// Check for valid XML at 6.x.
$this->drupalGet('drupal/release-history/' . $this->org . '_module/6.x');
$headers = $this->drupalGetHeaders();
$this->assertEqual($headers['content-type'], 'text/xml; charset=utf-8', 'Found valid XML for 6.x.');
$this->validateProjectXml($this->xpath('//project'), '6.x');
$this->assertNoFieldByXpath('//version', 'master', 'No release found for "master."');
// Check 6.x releases.
$releases = $this->xpath('//release');
$this->assertEqual(count($releases), 2, 'Found 2 release for 6.x.');
foreach ($releases as $release) {
$this->validateReleaseXml($release);
}
// Check 6.x files.
$files = $this->xpath('//file');
$this->assertEqual(count($files), 2, 'Found 2 file for 6.x');
foreach ($files as $file) {
$this->validateFileXml($file);
}
// Check for valid XML at 7.x.
$this->drupalGet('drupal/release-history/' . $this->org . '_module/7.x');
$headers = $this->drupalGetHeaders();
$this->assertEqual($headers['content-type'], 'text/xml; charset=utf-8', 'Found valid XML for 7.x.');
$this->validateProjectXml($this->xpath('//project'), '7.x');
$this->assertNoFieldByXpath('//version', 'master', 'No release found for "master."');
// Check 7.x releases.
$releases = $this->xpath('//release');
$this->assertEqual(count($releases), 5, 'Found 5 releases for 7.x');
foreach ($releases as $release) {
$this->validateReleaseXml($release);
}
// Check 7.x files.
$files = $this->xpath('//file');
$this->assertEqual(count($files), 5, 'Found 5 files for 7.x');
foreach ($files as $file) {
$this->validateFileXml($file);
}
// Check that there are no results for 8.x.
$this->drupalGet('drupal/release-history/' . $this->org . '_module/8.x');
$headers = $this->drupalGetHeaders();
$this->assertEqual($headers['content-type'], 'text/xml; charset=utf-8', 'Found valid XML for 8.x.');
$this->assertNoFieldByXpath('//release', NULL, 'No releases found for 8.x.');
$text = 'No release history was found for the requested project (' . $this->org . '_module).';
$this->assertFieldByXPath('//error', $text, 'Found "no release history" messaging for 8.x');
}
}
/**
* Tests Project Source: GitHub file generation.
*/
class ProjectSrcGithubFileTests extends ProjectSrcGithubBaseCase {
public static function getInfo() {
return array(
'name' => 'Project Source GitHub - File',
'description' => 'Tests the Project Source GitHub file generation.',
'group' => 'Project Source GitHub',
);
}
function setUp() {
parent::setUp(array('project_src_github_test'));
}
/**
* Validates file metadata and validity.
*
* @param array $commitrefs
* An array of commit refs, keyed by ref (e.g. tag name or branch name).
* @param bool $file_exists
* Checks whether or not the file exists and is managed after requesting it.
*/
protected function validateFileMetadata(array $commitrefs, $file_exists) {
$root = 'public://project-src-github/drewpaul_inc/drewpaul_inc_module';
$module = $this->org . '_module';
foreach ($commitrefs as $ref => $info) {
$file = $root . '/' . $ref . '/download.tar.gz';
// Check that the file does not exist locally.
$this->assertFalse(file_exists($file), format_string('No local file for !ref yet.', array(
'!ref' => $ref,
)));
// Then get the file and check the response, content type, and filename.
$this->drupalGet(file_create_url($file));
$headers = $this->drupalGetHeaders();
$this->assertEqual($headers[':status'], 'HTTP/1.1 200 OK', 'File returned a 200.');
$this->assertEqual($headers['content-type'], 'application/x-gzip', format_string('Found a valid tar archive for !ref.', array(
'!ref' => $ref,
)));
$filename = $module . '-' . $ref . '.tar.gz';
$this->assertEqual($headers['content-disposition'], 'attachment; filename="' . $filename . '"', format_string('File name header set to !filename', array(
'!filename' => $filename,
)));
// If desired, check that the file now exists.
if ($file_exists) {
// Then check that the file does exist locally.
$this->assertTrue(file_exists($file), format_string('File successfully saved for !ref.', array(
'!ref' => $ref,
)));
// Check that the file is being managed.
$managed_file = _project_src_github_load_file_by_uri($file);
$this->assertEqual($managed_file->filename, 'download.tar.gz', 'Found a valid entry in the managed file system.');
// Clean up the file.
file_delete($managed_file);
}
else {
// Ensure a file was not created.
$this->assertTrue(!file_exists($file), format_string('File was not saved for !ref.', array(
'!ref' => $ref,
)));
// Ensure no managed file entry was created.
$this->assertFalse(_project_src_github_load_file_by_uri($file), 'Found no entry in the managed file system.');
}
}
}
protected function validateFileInfoMetadata(array $commitrefs) {
$project = $this->org . '_module';
$root = 'public://project-src-github/' . $this->org . '/' . $project;
$show = $this->github->show($this->org, $project, '');
$date = $show['commit']['author']['date'];
foreach ($commitrefs as $ref => $info) {
$get = $root . '/' . $ref . '/download.tar.gz';
// Test a managed file.
$response = drupal_http_request(file_create_url($get));
$tarball = $response->data;
// Save the tarball to the temp directory.
$tar_name = md5($get) . '.tar.gz';
$tar_file = file_unmanaged_save_data($tarball, 'temporary://' . $tar_name);
// Get an archiver and extract the GitHub tarball into its own directory.
$tar_extracted = 'temporary://' . $tar_name . '.extract';
file_prepare_directory($tar_extracted, FILE_CREATE_DIRECTORY);
$tar = archiver_get_archiver($tar_file);
$tar->extract($tar_extracted);
$source = $tar_extracted . '/' . $project;
// Extract the info file metadata.
$info_file = $source . '/' . $project . '.info';
$this->assertTrue(file_exists($info_file), 'Found the project info file.');
$info = drupal_parse_info_file($info_file);
// Check all relevant info file properties.
$expected_url = $this->getAbsoluteUrl('drupal/release-history');
$this->assertEqual($info['project status url'], $expected_url, format_string('Project status URL !url included in .info file.', array(
'!url' => $expected_url,
)));
$this->assertEqual($info['project'], $project, format_string('Project !project included in .info file.', array(
'!project' => $project,
)));
$this->assertEqual($info['version'], $ref, format_string('Version !version included in .info file.', array(
'!version' => $ref,
)));
$this->assertEqual($info['datestamp'], strtotime($date), format_string('Date !date included in .info file.', array(
'!date' => $date,
)));
// Clean up all remaining temporary files.
file_unmanaged_delete($tar_file);
_project_src_github_delete_recursive($tar_extracted);
if ($managed_file = _project_src_github_load_file_by_uri($get)) {
file_delete($managed_file);
}
}
}
/**
* Tests tagged and branch-based release tar archive metadata.
*/
public function testProjectSourceProjectFiles() {
// Ensure that all tagged releases have valid archives.
$tags = $this->github->tags($this->org, $this->org . '_module');
$this->validateFileMetadata($tags, TRUE);
// Ensure that all branches have valid archives.
$branches = $this->github->branches($this->org, $this->org . '_module');
unset($branches['master']);
$this->validateFileMetadata($branches, FALSE);
}
/**
* Tests release .info packaging rewriting.
*/
public function testProjectSourceProjectInfoMetadata() {
// Ensure all tagged releases' archives have properly packaged .info files.
$tags = $this->github->tags($this->org, $this->org . '_module');
$this->validateFileInfoMetadata($tags);
// Ensure all branch releases' archives have properly packaged .info files.
$branches = $this->github->branches($this->org, $this->org . '_module');
unset($branches['master']);
$this->validateFileMetadata($branches, FALSE);
}
/**
* Tests that private repositories are loaded using https basic auth.
*/
public function testProjectSourcePrivateRepositoryFiles() {
// Set GitHub API credentials.
$un = 'drewpaul';
variable_set('github_api_username', $un);
$pw = 'Dr3Wp4ulP@SSW0RD';
variable_set('github_api_password', $pw);
// Set our custom user-agent to ensure we hit the right DB/prefix/instance.
if (preg_match('/simpletest\d+/', $this->databasePrefix, $matches)) {
variable_set('project_src_github_ua_string', drupal_generate_test_ua($matches[0]));
}
// Ensure that basic auth credentials are included for private repos.
$path = $this->getAbsoluteUrl('github-api-file-test');
$response = _project_src_github_file_as_string($path, TRUE);
$this->assertTrue(strpos($response, $un) !== FALSE && strpos($response, $pw) !== FALSE, 'HTTP basic auth credentials included for private repo.');
// Ensure that basic auth credentials are not included for public repos.
$response = _project_src_github_file_as_string($path, FALSE);
$this->assertTrue(strpos($response, $un) === FALSE && strpos($response, $pw) === FALSE, 'HTTP basic auth credentials excluded for public repo.');
}
}
/**
* Tests the Project Source: GitHub admin interface.
*/
class ProjectSrcGithubAdminTests extends ProjectSrcGithubBaseCase {
public static function getInfo() {
return array(
'name' => 'Project Source GitHub - Admin',
'description' => 'Tests the Project Source GitHub admin interface.',
'group' => 'Project Source GitHub',
);
}
function setUp(array $modules = array()) {
parent::setUp($modules);
// Create a user and log it in.
$this->admin_user = $this->drupalCreateUser(array(
'access administration pages',
'administer site configuration',
'administer project src',
'administer project github src',
));
$this->drupalLogin($this->admin_user);
}
/**
* Validates a select field's options list against what's expected for the API
* version of the project.
*
* @param string $api
* The API version of the project.
* @param array $options
* An array of SimpleXMLElements representing <option> tags.
*/
protected function validateOptionsList($api, $options) {
$branches = $this->github->branches('', '', '');
$expected = array();
foreach ($branches as $branch => $info) {
if (preg_match_all("/^(\\d+).x-(\\d+).x$/is", $branch, $versions)) {
$api_version = $versions[1][0];
$major_version = $versions[2][0];
$expected[$api_version . '.x'][$major_version] = TRUE;
}
}
$count = 0;
foreach ($options as $option) {
if (!empty($option['value'])) {
$this->assertTrue(isset($expected[$api][(string) $option['value']]), 'Provided option corresponds to valid branch.');
$count++;
}
}
$this->assertEqual(count($expected[$api]), $count, 'Number of options provided matches number of valid branches.');
}
/**
* Tests that only valid branch names are available as selections for both
* supported and recommended releases in the Project Source configuration UI.
*/
public function testProjectSourceAdminUI() {
// Load a list of projects, keyed by API version.
$branches = $this->github->branches('', '', '');
$projects = array();
foreach ($branches as $branch => $info) {
if (preg_match_all("/^(\\d+).x-(\\d+).x$/is", $branch, $versions)) {
$api_version = $versions[1][0];
if (!isset($projects[$api_version . '.x'])) {
$projects[$api_version . '.x'] = $this->github->repositories($this->org);
}
}
}
// Load the admin page for project source.
$this->drupalGet('admin/config/development/project-src');
foreach ($projects as $api => $list) {
foreach ($list as $short_name => $project) {
// Verify that a field exists for supported majors for this API version.
$name = 'project_src_project_settings[' . $short_name . '][' . $api . '][supported_majors][]';
$this->assertFieldByName($name, NULL, format_string('Found supported majors field for !project !api', array('!project' => $short_name, '!api' => $api)));
// Load the field we found above and validate its options list.
$field = $this->xpath($this->constructFieldXpath('name', $name));
$this->validateOptionsList($api, $this->getAllOptions($field[0]));
// Verify that the recommended major field exists for this API version.
$name = 'project_src_project_settings[' . $short_name . '][' . $api . '][recommended_major]';
$this->assertFieldByName($name, NULL, format_string('Found recommended major field for !project !api', array('!project' => $short_name, '!api' => $api)));
// Load the field we found above and validate its options list.
$field = $this->xpath($this->constructFieldXpath('name', $name));
$this->validateOptionsList($api, $this->getAllOptions($field[0]));
}
}
// Load our admin UI, ensure our field exists, is save-able.
$var = 'project_src_github_org';
$this->drupalGet('admin/config/development/project-src/github');
$this->assertFieldByName($var, NULL, 'Found GitHub organization field.');
$this->drupalPost(NULL, array($var => $this->org), 'Save configuration');
$this->assertFieldByName($var, $this->org, 'GitHub organization field saved.');
$this->assertEqual(variable_get($var, ''), $this->org, 'GitHub organization variable saved.');
// Set a cached item, ensure it exists in cache.
$cid = 'project_src_github_anything';
cache_set($cid, 'data', 'cache');
$this->assertNotEqual(FALSE, cache_get($cid, 'cache'), 'Project Source GitHub cache item set.');
// Clear the cache through the GUI and ensure it was actually cleared.
$this->drupalPost(NULL, array(), 'Clear project cache');
$this->assertFalse(cache_get($cid, 'cache'), 'Project Source GitHub cache cleared via admin UI.');
}
}