-
Notifications
You must be signed in to change notification settings - Fork 24
/
RoboFile.php
901 lines (818 loc) · 25.7 KB
/
RoboFile.php
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
<?php
/**
* This is project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
use CzProject\GitPhp\Git;
use Robo\Result;
use Robo\ResultData;
use Robo\Tasks;
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Yaml;
/**
* RoboFile is the main entry point for Robo commands.
*/
class RoboFile extends Tasks {
/**
* @var string
* The terminus executable path.
*/
public static $TERMINUS_EXE = '/usr/local/bin/terminus';
/**
* @var \DateTime
* When this run started.
*/
public DateTime $started;
/**
* Class constructor.
*/
public function __construct() {
$this->started = new DateTime();
require_once 'vendor/autoload.php';
}
/**
* Delete sites created in this test run.
*/
public function testDeleteSites() {
$home = $_SERVER['HOME'];
$file_contents = file_get_contents("$home/.robo-sites-created");
$filenames = explode("\n", $file_contents);
foreach ($filenames as $site_name) {
if ($site_name) {
$this->output()->writeln("Deleting site $site_name.");
$this->taskExec(static::$TERMINUS_EXE)
->args('site:delete', '-y', $site_name)
->run();
}
}
}
/**
* Run the full test suite for this project.
*/
public function testFull(int $drupal_version = 9, string $site_name = NULL) {
// Get the right constraint based on current branch/tag.
$constraint = $this->getCurrentConstraint();
// Ensure terminus 3 is installed.
$this->testCheckT3();
// This is a GitHub secret.
$options = isset($_SERVER['TERMINUS_ORG']) ? ['org' => $_SERVER['TERMINUS_ORG']] : [];
// Prepare the site name if not already set.
if (empty($site_name)) {
$site_name = substr(\uniqid('test-'), 0, 12);
if ($_SERVER['GITHUB_RUN_NUMBER']) {
// Ensure that 2 almost parallel runs do not collide.
$site_name .= '-' . $drupal_version . '-'. $_SERVER['GITHUB_RUN_NUMBER'];
}
}
// Create site, set connection mode to git and clone it to local.
$this->testCreateSite($site_name, $options);
$this->testConnectionGit($site_name, 'dev', 'git');
$this->testCloneSite($site_name);
$this->testAllowPlugins($site_name, $drupal_version);
$this->testPhpVersion($site_name, $drupal_version);
// If received Drupal 10, upgrade the recently created site to Drupal 10.
if ($drupal_version === 10) {
// @todo Make it downgrade to Drupal 9 when Drupal 10 becomes the default version.
$this->testUpgradeToDrupal10($site_name);
}
// Composer require the corresponding modules, push to Pantheon and install the site.
$this->testRequireSolr($site_name, $constraint);
$this->testGitPush($site_name);
$this->testConnectionGit($site_name, 'dev', 'sftp');
$this->testSiteInstall($site_name);
$this->testConnectionGit($site_name, 'dev', 'git');
try {
// This should fail because Solr has not been enabled on the environment yet.
$this->testModuleEnable($site_name);
}
catch (\Exception $e) {
\Kint::dump($e);
exit(1);
}
// Enable Solr for this site and set solr version to 8, then try enabling the module again.
$this->setSiteSearch($site_name, 'enable');
$this->testEnvSolr($site_name);
$this->testGitPush($site_name, 'Changes to pantheon.yml file.');
// This should succeed now that Solr has been enabled.
$this->testModuleEnable($site_name);
// Test all the Solr things.
$this->testSolrEnabled($site_name);
// Test creating Solr index.
$this->testSolrIndexCreate($site_name, 'dev');
// Test running the reload
$this->testPantheonSolrReload($site_name, 'dev');
$this->testSolrReload($site_name, 'dev');
// Test select query.
$this->testSolrSelect($site_name, 'dev');
// Finally, run Solr diagnose.
$this->testSolrDiagnose($site_name, 'dev');
$this->output()->write( 'All done! 🎉' );
return ResultData::EXITCODE_OK;
}
/**
* Get current composer constraint depending on whether we're on a tag, a branch or a PR.
*/
protected function getCurrentConstraint(): string {
$branch = trim(shell_exec('git rev-parse --abbrev-ref HEAD'));
if ($branch !== 'HEAD') {
return "${branch}-dev";
} else {
$tag = trim(shell_exec('git describe --exact-match --tags $(git log -n1 --pretty=\'%h\')'));
if ($tag) {
return $tag;
} else {
// Maybe we are on a PR.
$branch = $_SERVER['GITHUB_HEAD_REF'];
$branch_parts = explode('/', $branch);
$branch = end($branch_parts);
if ($branch) {
return "${branch}-dev";
}
}
}
// Final fallback, return "^8";
return '^8';
}
/**
* Ensure terminus 3 is installed, otherwise offer installing it using Homebrew.
*/
public function testCheckT3() {
if (!file_exists(static::$TERMINUS_EXE) || !is_executable(static::$TERMINUS_EXE)) {
$this->confirm(
'This demo makes extensive use of the Terminus 3 phar. Can I install it for you using homebrew?'
);
$result = $this->taskExec('brew install pantheon-systems/external/terminus')->run();
if (!$result->wasSuccessful()) {
exit(1);
}
// @todo check for build tools plugin "wait" command.
}
return ResultData::EXITCODE_OK;
}
/**
* Create site in Pantheon if it doesn't exist. Return site info.
*
* @param string $site_name
* The machine name of the site to create.
*
* @return \Robo\Result
*/
public function testCreateSite(string $site_name, array $options = ['org' => NULL]) {
$site_info = $this->siteInfo($site_name);
if (empty($site_info)) {
$home = $_SERVER['HOME'];
$toReturn = $this->taskExec(static::$TERMINUS_EXE)
->args('site:create', $site_name, $site_name, 'drupal-composer-managed');
if ( !empty( $options['org'] ) ) {
$toReturn->option('org', $options['org']);
}
$toReturn->run();
$this->waitForWorkflow($site_name);
$site_info = $this->siteInfo($site_name);
// Write to $HOME/.robo-sites-created to delete them later.
exec("echo $site_name >> $home/.robo-sites-created");
}
return $site_info;
}
/**
* Wait for the given workflow to finish.
*
* @param string $site_name
*/
public function waitForWorkflow(string $site_name, string $env = 'dev') {
$this->output()->write('Checking workflow status', true);
exec(
"terminus workflow:info:status $site_name.$env",
$info
);
$info = $this->cleanUpInfo( $info );
$this->output()->write( $info['workflow'], true );
// Wait for workflow to finish only if it hasn't already. This prevents the workflow:wait command from unnecessarily running for 260 seconds when there's no workflow in progress.
if ( $info['status'] !== 'succeeded' ) {
$this->output()->write('Waiting for platform', true);
exec(
"terminus build:workflow:wait --max=260 $site_name.$env",
$finished,
$status
);
}
if ($this->output()->isVerbose()) {
\Kint::dump(get_defined_vars());
}
$this->output()->writeln('');
}
/**
* Takes the output from a workflow:info:status command and converts it into a human-readable and easily parseable array.
*
* @param array $info Raw output from 'terminus workflow:info:status'
*
* @return array An array of workflow status info.
*/
private function cleanUpInfo( array $info ) : array {
// Clean up the workflow status data and assign values to an array so it's easier to check.
foreach ($info as $line => $value) {
$ln = array_values( array_filter( explode( " ", trim( $value ) ) ) );
// Skip lines with only one value. This filters out the ASCII dividers output by the command.
if ( count( $ln ) > 1 ) {
if ( in_array( $ln[0], [ 'Started At', 'Finished At' ] ) ) {
$ln[0] = trim( str_replace( 'At', '', $ln[0] ) );
// Convert times to unix timestamps for easier use later.
$ln[1] = strtotime( $ln[1] );
}
$info[ str_replace( ' ', '-', strtolower( $ln[0] ) ) ] = trim( $ln[1] );
}
// Remove the processed line.
unset( $info[ $line ] );
}
return $info;
}
/**
* Run "terminus solr:enable" or "terminus solr:disable" on the given site.
*
* @param string $site_name
* The machine name of the site to enable/disable Solr on.
* @param string $value
* The value to pass to the command.
*/
public function setSiteSearch(string $site_name, $value = 'enable') {
$this->taskExec(static::$TERMINUS_EXE)
->args('solr:' . $value, $site_name)
->run();
}
/**
* Set environment connection mode to git or sftp.
*
* @param string $site_name
* The machine name of the site to set the connection mode.
* @param string $env
* The environment to set the connection mode.
* @param string $connection
* The connection mode to set (git/sftp).
*/
public function testConnectionGit(string $site_name, string $env = 'dev', string $connection = 'git') {
$this->taskExec('terminus')
->args('connection:set', $site_name . '.' . $env, $connection)
->run();
}
/**
* Use terminus local:clone to get a copy of the remote site.
*
* @param string $site_name
* The machine name of the site to clone.
*
* @return \Robo\Result
*/
public function testCloneSite(string $site_name) {
if (!is_dir($this->getSiteFolder($site_name))) {
$toReturn = $this->taskExec(static::$TERMINUS_EXE)
->args('local:clone', $site_name)
->run();
return $toReturn;
}
return ResultData::EXITCODE_OK;
}
/**
* Add allow plugins section to composer.
*
* @param string $site_name
* The machine name of the site to add the allow plugins section to.
* @param int $drupal_version
* The major version of Drupal to use.
*/
public function testAllowPlugins(string $site_name, int $drupal_version) {
$plugins = [
'drupal/core-project-message',
];
if ($drupal_version === 10) {
$plugins[] = 'phpstan/extension-installer';
// @todo Remove once all of the modules have been correctly upgraded.
$plugins[] = 'mglaman/composer-drupal-lenient';
}
if (count($plugins)) {
$site_folder = $this->getSiteFolder($site_name);
chdir($site_folder);
foreach ($plugins as $plugin_name) {
$this->taskExec('composer')
->args(
'config',
'--no-interaction',
'allow-plugins.' . $plugin_name,
'true'
)
->run();
}
}
}
/**
* Upgrade given site to Drupal 10.
*
* @param string $site_name
* The machine name of the site to downgrade.
*/
public function testUpgradeToDrupal10(string $site_name) {
$site_folder = $this->getSiteFolder($site_name);
chdir($site_folder);
// Remove composer lock.
$this->taskExec('rm')
->args('composer.lock')
->run();
$this->taskExec('composer')
->args(
'config',
'minimum-stability',
'dev'
)
->run();
$this->taskExec('composer')
->args(
'config',
'platform.php',
'8.1'
)
->run();
$this->taskExec('composer')
->args(
'require',
'--no-update',
'drupal/core-recommended:^10',
'drupal/core-project-message:^10',
'drupal/core-composer-scaffold:^10',
'pantheon-systems/drupal-integrations:^10',
'mglaman/composer-drupal-lenient'
)
->run();
$this->taskExec('composer')
->args(
'require',
'--no-update',
'--dev',
'drupal/core-dev:^10'
)
->run();
$this->taskExec('composer')
->args('update')
->run();
$this->taskExec('composer')
->args(
'config',
'--merge',
'--json',
'extra.drupal-lenient.allowed-list',
'["drupal/search_api_pantheon"]'
)
->run();
return ResultData::EXITCODE_OK;
}
/**
* Downgrade given site to Drupal 8.
*
* @param string $site_name
* The machine name of the site to downgrade.
*/
public function testDowngradeToDrupal8(string $site_name) {
$site_folder = $this->getSiteFolder($site_name);
chdir($site_folder);
// Remove composer lock.
$this->taskExec('rm')
->args('composer.lock')
->run();
$this->taskExec('composer')
->args(
'require',
'--no-update',
'-W',
'drupal/core-recommended:^8',
'pantheon-systems/drupal-integrations:^8'
)
->run();
$this->taskExec('composer')
->args('update')
->run();
return ResultData::EXITCODE_OK;
}
/**
* Composer require the Solr related modules.
*
* @param string $site_name
* The machine name of the site to require the Solr modules.
* @param string $constraint
* The constraint to use for the search_api_pantheon module.
*/
public function testRequireSolr(string $site_name, string $constraint = '^8') {
$site_folder = $this->getSiteFolder($site_name);
chdir($site_folder);
// Always test again latest version of search_api_solr.
$this->taskExec('composer')
->args(
'require',
'drupal/search_api_solr:dev-4.x',
)
->run();
$this->taskExec('composer')
->args(
'require',
'pantheon-systems/search_api_pantheon ' . $constraint,
)
->run();
return ResultData::EXITCODE_OK;
}
/**
* Return folder in local machine for given site name.
*
* @param string $site_name
* The machine name of the site to get the folder for.
*
* @return string
* Full path to the site folder.
*/
protected function getSiteFolder(string $site_name) {
return $_SERVER['HOME'] . '/pantheon-local-copies/' . $site_name;
}
/**
* Add all changes to the git repository, commit and push.
*
* @param string $site_name
* The machine name of the site to commit and push.
* @param string $commit_msg
* The commit message to use.
*/
public function testGitPush(string $site_name, string $commit_msg = 'Changes committed from demo script.') {
$site_folder = $this->getSiteFolder($site_name);
chdir($site_folder);
try {
$git = new Git();
$repo = $git->open($site_folder);
if ($repo->hasChanges()) {
$repo->addAllChanges();
$repo->commit($commit_msg);
}
$result = $this->taskExec('git push origin master')
->run();
if ($result instanceof Result && !$result->wasSuccessful()) {
\Kint::dump($result);
throw new \Exception("error occurred");
}
}
catch (\Exception $e) {
$this->output()->write($e->getMessage());
return ResultData::EXITCODE_ERROR;
}
catch (\Throwable $t) {
$this->output()->write($t->getMessage());
return ResultData::EXITCODE_ERROR;
}
$this->waitForWorkflow($site_name);
return ResultData::EXITCODE_OK;
}
/**
* Install the Drupal site in Pantheon.
*
* @param string $site_name
* The machine name of the site to install.
* @param string $env
* The environment to install the site in.
* @param string $profile
* The Drupal profile to use during site installation.
*/
public function testSiteInstall(string $site_name, string $env = 'dev', string $profile = 'demo_umami') {
$this->taskExec(static::$TERMINUS_EXE)
->args('drush', $site_name . '.' . $env, '--', 'site:install', $profile, '-y')
->options([
'account-name' => 'admin',
'site-name' => $site_name,
'locale' => 'en',
])
->run();
$this->waitForWorkflow($site_name);
return ResultData::EXITCODE_OK;
}
/**
* Enable solr modules in given Pantheon site.
*
* @param string $site_name
* The machine name of the site to enable solr modules.
* @param string $env
* The environment to enable the modules in.
*/
public function testModuleEnable(string $site_name, string $env = 'dev') {
$this->taskExec(static::$TERMINUS_EXE)
->args(
'drush',
$site_name . '.' . $env,
'cr'
)
->run();
$this->taskExec(static::$TERMINUS_EXE)
->args(
'drush',
$site_name . '.' . $env,
'pm-uninstall',
'search',
)
->run();
$this->waitForWorkflow($site_name);
$this->taskExec(static::$TERMINUS_EXE)
->args(
'drush',
$site_name . '.' . $env,
'--',
'pm-enable',
'--yes',
'search_api_pantheon',
'search_api_pantheon_admin',
'search_api_solr_admin'
)
->run();
$this->taskExec(static::$TERMINUS_EXE)
->args(
'drush',
$site_name . '.' . $env,
'cr'
)
->run();
}
/**
* Run through various diagnostics to ensure that Solr8 is enabled and working.
*
* @param string $site_name
* The machine name of the site to run the diagnostics on.
* @param string $env
* The environment to run the diagnostics on.
*/
public function testSolrEnabled( string $site_name, string $env = 'dev' ) {
try {
// Attempt to ping the Pantheon Solr server.
$this->output()->write('Attempting to ping the Solr server...', true);
$ping = $this->taskExec( static::$TERMINUS_EXE )
->args(
'drush',
"$site_name.$env",
'--',
'search-api-pantheon:ping'
)
->run();
if ( $ping instanceof Result && ! $ping->wasSuccessful() ) {
\Kint::dump( $ping );
throw new \Exception( 'An error occurred attempting to ping Solr server' );
}
// Check that Solr8 is enabled.
$this->output()->write('Checking for Solr8 search API server...', true);
exec(
"terminus remote:drush $site_name.$env -- search-api-server-list | grep pantheon_solr8",
$server_list
);
if ( stripos( $server_list[0], 'enabled' ) === false ) {
\Kint::dump( $server_list );
throw new \Exception( 'An error occurred checking that Solr8 was enable.d' );
}
}
catch (\Exception $e) {
$this->output()->write($e->getMessage());
return ResultData::EXITCODE_ERROR;
}
catch (\Throwable $t) {
$this->output()->write($t->getMessage());
return ResultData::EXITCODE_ERROR;
}
return ResultData::EXITCODE_OK;
}
/**
* Run drush search-api-pantheon:diagnose to complete the Solr8 diagnostic.
*
* @param string $site_name
* The machine name of the site to run the diagnostics on.
* @param string $env
* The environment to run the diagnostics on.
*/
public function testSolrDiagnose( string $site_name, string $env = 'dev' ) {
try {
// Run a diagnose command to make sure everything is okay.
$this->output()->write('Running search-api-pantheon:diagnose...', true);
$diagnose = $this->taskExec( static::$TERMINUS_EXE )
->args(
'drush',
"$site_name.$env",
'--',
'search-api-pantheon:diagnose',
'-v'
)
->run();
if ( $diagnose instanceof Result && ! $diagnose->wasSuccessful() ) {
\Kint::dump( $diagnose );
throw new \Exception( 'An error occurred while running Solr search diagnostics.' );
}
}
catch (\Exception $e) {
$this->output()->write($e->getMessage());
return ResultData::EXITCODE_ERROR;
}
return ResultData::EXITCODE_OK;
}
/**
* Helper function to demo login in to the Drupal site using a login link.
*
* @param string $site_name
* The machine name of the site to login to.
* @param string $env
* The environment to login to.
*/
public function demoLoginBrowser(string $site_name, string $env = 'dev') {
exec(
'terminus drush ' . $site_name . '.' . $env . ' -- uli admin',
$finished,
$status
);
$finished = trim(join('', $finished));
$this->output()->writeln($finished);
$this->_exec('open ' . $finished);
}
/**
* @param string $text
*/
protected function say($text, string $step_id = 'narration') {
$now = new \DateTime();
$filename = 'narration-' .
$now->diff($this->started)->format('%I-%S-%F') . '-' . $step_id . '.m4a';
$this->output->writeln('/Users/Shared/' . $filename);
return (new Process([
'/usr/bin/say',
'--voice=Daniel',
"--output-file={$filename}",
'--file-format=m4af',
$text,
], '/Users/Shared'))
->enableOutput()
->setTty(TRUE);
}
/**
* Get information about the given site.
*
* @param string $site_name
* The machine name of the site to get information about.
*
* @return mixed|null
*/
protected function siteInfo(string $site_name) {
try {
exec(static::$TERMINUS_EXE . ' site:info --format=json ' . $site_name, $output, $status);
if (!empty($output)) {
$result = json_decode(join("", $output), TRUE, 512, JSON_THROW_ON_ERROR);
return $result;
}
}
catch (\Exception $e) {
}
catch (\Throwable $t) {
}
return NULL;
}
/**
* Set correct PHP version for the given site.
*
* @param string $site_name
* The machine name of the site to set the Solr version for.
*/
public function testPhpVersion(string $site_name, int $drupal_version) {
$site_folder = $this->getSiteFolder($site_name);
$pantheon_yml_contents = Yaml::parseFile($site_folder . '/pantheon.yml');
if ($drupal_version === 10) {
$pantheon_yml_contents['php_version'] = 8.2;
} else {
$pantheon_yml_contents['php_version'] = 8.1;
}
$pantheon_yml_contents = Yaml::dump($pantheon_yml_contents);
file_put_contents($site_folder . '/pantheon.yml', $pantheon_yml_contents);
$this->output->writeln($pantheon_yml_contents);
}
/**
* Set correct Solr version for the given site.
*
* @param string $site_name
* The machine name of the site to set the Solr version for.
*/
public function testEnvSolr(string $site_name) {
$site_folder = $this->getSiteFolder($site_name);
$pantheon_yml_contents = Yaml::parseFile($site_folder . '/pantheon.yml');
$pantheon_yml_contents['search'] = ['version' => 8];
$pantheon_yml_contents = Yaml::dump($pantheon_yml_contents);
file_put_contents($site_folder . '/pantheon.yml', $pantheon_yml_contents);
$this->output->writeln($pantheon_yml_contents);
}
/**
* Create a Solr index based on the configuration under .ci/config.
*
* @param string $site_name
* The machine name of the site to create the index in.
* @param string $env
* The environment to create the index in.
*/
public function testSolrIndexCreate(string $site_name, string $env = 'dev') {
$result = $this->taskExec( static::$TERMINUS_EXE )
->args(
'drush',
"$site_name.$env",
'--',
'cim',
'--partial',
'--source=modules/contrib/search_api_pantheon/.ci/config',
'-y'
)
->run();
if (!$result->wasSuccessful()) {
exit(1);
}
// Index new solr.
$result = $this->taskExec( static::$TERMINUS_EXE )
->args(
'drush',
"$site_name.$env",
'--',
'sapi-i'
)
->run();
if (!$result->wasSuccessful()) {
exit(1);
}
}
public function testPantheonSolrReload(string $site_name, string $env = 'dev') {
$result = $this->taskExec( static::$TERMINUS_EXE )
->args(
'drush',
"$site_name.$env",
'--',
'search-api-pantheon:reloadSchema',
)
->run();
if (!$result->wasSuccessful()) {
exit(1);
}
}
public function testSolrReload(string $site_name, string $env = 'dev') {
$result = $this->taskExec( static::$TERMINUS_EXE )
->args(
'drush',
"$site_name.$env",
'--',
'search-api-solr:reload',
'pantheon_solr8'
)
->run();
if (!$result->wasSuccessful()) {
exit(1);
}
}
/**
* Use search-api-pantheon:select command to ensure both Drupal index and the actual Solr index have the same amount of items.
*
* @param string $site_name
* The machine name of the site to run the tests on.
* @param string $env
* The environment to run the tests on.
*/
public function testSolrSelect(string $site_name, string $env = 'dev') {
$sapi_s = new Process([
static::$TERMINUS_EXE,
'drush',
"$site_name.$env",
'--',
'sapi-s',
'--field=Indexed',
]);
$total_indexed = 0;
$sapi_s->run();
if ($sapi_s->isSuccessful()) {
$result = $sapi_s->getOutput();
$result_parts = explode("\n", $result);
foreach ($result_parts as $part) {
if (is_numeric(trim($part))) {
$total_indexed = trim($part);
}
}
}
$saps = new Process([
static::$TERMINUS_EXE,
'drush',
"$site_name.$env",
'--',
'saps',
'*:*'
]);
$num_found = 0;
$saps->run();
if ($saps->isSuccessful()) {
$result = $saps->getOutput();
$result_parts = explode("\n", $result);
foreach ($result_parts as $part) {
if (strpos($part, 'numFound') !== FALSE) {
$num_found = trim(str_replace('"numFound":', '', $part), ' ,');
break;
}
}
}
if ($total_indexed && $num_found && $total_indexed != $num_found) {
$this->output->writeln('Solr indexing error. Total indexed: ' . $total_indexed . ' but found: ' . $num_found);
exit(1);
}
}
}