BuildTestBase.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. namespace Drupal\BuildTests\Framework;
  3. use Behat\Mink\Driver\Goutte\Client;
  4. use Behat\Mink\Driver\GoutteDriver;
  5. use Behat\Mink\Mink;
  6. use Behat\Mink\Session;
  7. use Drupal\Component\FileSystem\FileSystem as DrupalFilesystem;
  8. use Drupal\Tests\PhpunitCompatibilityTrait;
  9. use PHPUnit\Framework\TestCase;
  10. use Symfony\Component\BrowserKit\Client as SymfonyClient;
  11. use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
  12. use Symfony\Component\Finder\Finder;
  13. use Symfony\Component\Lock\Factory;
  14. use Symfony\Component\Lock\Store\FlockStore;
  15. use Symfony\Component\Process\PhpExecutableFinder;
  16. use Symfony\Component\Process\Process;
  17. /**
  18. * Provides a workspace to test build processes.
  19. *
  20. * If you need to build a file system and then run a command from the command
  21. * line then this is the test framework for you.
  22. *
  23. * Tests using this interface run in separate processes.
  24. *
  25. * Tests can perform HTTP requests against the assembled codebase.
  26. *
  27. * The results of these HTTP requests can be asserted using Mink.
  28. *
  29. * This framework does not use the same Mink extensions as BrowserTestBase.
  30. *
  31. * Features:
  32. * - Provide complete isolation between the test runner and the site under test.
  33. * - Provide a workspace where filesystem build processes can be performed.
  34. * - Allow for the use of PHP's build-in HTTP server to send requests to the
  35. * site built using the filesystem.
  36. * - Allow for commands and HTTP requests to be made to different subdirectories
  37. * of the workspace filesystem, to facilitate comparison between different
  38. * build results, and to support Composer builds which have an alternate
  39. * docroot.
  40. * - Provide as little framework as possible. Convenience methods should be
  41. * built into the test, or abstract base classes.
  42. * - Allow parallel testing, using random/unique port numbers for different HTTP
  43. * servers.
  44. * - Allow the use of PHPUnit-style (at)require annotations for external shell
  45. * commands.
  46. *
  47. * We don't use UiHelperInterface because it is too tightly integrated to
  48. * Drupal.
  49. */
  50. abstract class BuildTestBase extends TestCase {
  51. use ExternalCommandRequirementsTrait;
  52. use PhpunitCompatibilityTrait;
  53. /**
  54. * The working directory where this test will manipulate files.
  55. *
  56. * Use getWorkspaceDirectory() to access this information.
  57. *
  58. * @var string
  59. *
  60. * @see self::getWorkspaceDirectory()
  61. */
  62. private $workspaceDir;
  63. /**
  64. * The process that's running the HTTP server.
  65. *
  66. * @var \Symfony\Component\Process\Process
  67. *
  68. * @see self::standUpServer()
  69. * @see self::stopServer()
  70. */
  71. private $serverProcess = NULL;
  72. /**
  73. * Default to destroying build artifacts after a test finishes.
  74. *
  75. * Mainly useful for debugging.
  76. *
  77. * @var bool
  78. */
  79. protected $destroyBuild = TRUE;
  80. /**
  81. * The docroot for the server process.
  82. *
  83. * This stores the last docroot directory used to start the server process. We
  84. * keep this information so we can restart the server if the desired docroot
  85. * changes.
  86. *
  87. * @var string
  88. */
  89. private $serverDocroot = NULL;
  90. /**
  91. * Our native host name, used by PHP when it starts up the server.
  92. *
  93. * Requests should always be made to 'localhost', and not this IP address.
  94. *
  95. * @var string
  96. */
  97. private static $hostName = '127.0.0.1';
  98. /**
  99. * Port that will be tested.
  100. *
  101. * Generated internally. Use getPortNumber().
  102. *
  103. * @var int
  104. */
  105. private $hostPort;
  106. /**
  107. * A list of ports used by the test.
  108. *
  109. * Prevent the same process finding the same port by storing a list of ports
  110. * already discovered. This also stores locks so they are not released until
  111. * the test class is torn down.
  112. *
  113. * @var \Symfony\Component\Lock\LockInterface[]
  114. */
  115. private $portLocks = [];
  116. /**
  117. * The Mink session manager.
  118. *
  119. * @var \Behat\Mink\Mink
  120. */
  121. private $mink;
  122. /**
  123. * The most recent command process.
  124. *
  125. * @var \Symfony\Component\Process\Process
  126. *
  127. * @see ::executeCommand()
  128. */
  129. private $commandProcess;
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public static function setUpBeforeClass() {
  134. parent::setUpBeforeClass();
  135. static::checkClassCommandRequirements();
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. protected function setUp() {
  141. parent::setUp();
  142. static::checkMethodCommandRequirements($this->getName());
  143. $this->phpFinder = new PhpExecutableFinder();
  144. // Set up the workspace directory.
  145. // @todo Glean working directory from env vars, etc.
  146. $fs = new SymfonyFilesystem();
  147. $this->workspaceDir = $fs->tempnam(DrupalFilesystem::getOsTemporaryDirectory(), '/build_workspace_' . md5($this->getName() . microtime(TRUE)));
  148. $fs->remove($this->workspaceDir);
  149. $fs->mkdir($this->workspaceDir);
  150. $this->initMink();
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. protected function tearDown() {
  156. parent::tearDown();
  157. $this->stopServer();
  158. foreach ($this->portLocks as $lock) {
  159. $lock->release();
  160. }
  161. $ws = $this->getWorkspaceDirectory();
  162. $fs = new SymfonyFilesystem();
  163. if ($this->destroyBuild && $fs->exists($ws)) {
  164. // Filter out symlinks as chmod cannot alter them.
  165. $finder = new Finder();
  166. $finder->in($ws)
  167. ->directories()
  168. ->ignoreVCS(FALSE)
  169. ->ignoreDotFiles(FALSE)
  170. // composer script is a symlink and fails chmod. Ignore it.
  171. ->notPath('/^vendor\/bin\/composer$/');
  172. $fs->chmod($finder->getIterator(), 0775, 0000);
  173. $fs->remove($ws);
  174. }
  175. }
  176. /**
  177. * Get the working directory within the workspace, creating if necessary.
  178. *
  179. * @param string $working_dir
  180. * The path within the workspace directory.
  181. *
  182. * @return string
  183. * The full path to the working directory within the workspace directory.
  184. */
  185. protected function getWorkingPath($working_dir = NULL) {
  186. $full_path = $this->getWorkspaceDirectory();
  187. if ($working_dir) {
  188. $full_path .= '/' . $working_dir;
  189. }
  190. if (!file_exists($full_path)) {
  191. $fs = new SymfonyFilesystem();
  192. $fs->mkdir($full_path);
  193. }
  194. return $full_path;
  195. }
  196. /**
  197. * Set up the Mink session manager.
  198. *
  199. * @return \Behat\Mink\Session
  200. */
  201. protected function initMink() {
  202. // If the Symfony BrowserKit client can followMetaRefresh(), we should use
  203. // the Goutte descendent instead of ours.
  204. if (method_exists(SymfonyClient::class, 'followMetaRefresh')) {
  205. $client = new Client();
  206. }
  207. else {
  208. $client = new DrupalMinkClient();
  209. }
  210. $client->followMetaRefresh(TRUE);
  211. $driver = new GoutteDriver($client);
  212. $session = new Session($driver);
  213. $this->mink = new Mink();
  214. $this->mink->registerSession('default', $session);
  215. $this->mink->setDefaultSessionName('default');
  216. $session->start();
  217. return $session;
  218. }
  219. /**
  220. * Get the Mink instance.
  221. *
  222. * Use the Mink object to perform assertions against the content returned by a
  223. * request.
  224. *
  225. * @return \Behat\Mink\Mink
  226. * The Mink object.
  227. */
  228. public function getMink() {
  229. return $this->mink;
  230. }
  231. /**
  232. * Full path to the workspace where this test can build.
  233. *
  234. * This is often a directory within the system's temporary directory.
  235. *
  236. * @return string
  237. * Full path to the workspace where this test can build.
  238. */
  239. public function getWorkspaceDirectory() {
  240. return $this->workspaceDir;
  241. }
  242. /**
  243. * Assert that text is present in the error output of the most recent command.
  244. *
  245. * @param string $expected
  246. * Text we expect to find in the error output of the command.
  247. */
  248. public function assertErrorOutputContains($expected) {
  249. $this->assertStringContainsString($expected, $this->commandProcess->getErrorOutput());
  250. }
  251. /**
  252. * Assert that text is present in the output of the most recent command.
  253. *
  254. * @param string $expected
  255. * Text we expect to find in the output of the command.
  256. */
  257. public function assertCommandOutputContains($expected) {
  258. $this->assertStringContainsString($expected, $this->commandProcess->getOutput());
  259. }
  260. /**
  261. * Asserts that the last command ran without error.
  262. *
  263. * This assertion checks whether the last command returned an exit code of 0.
  264. *
  265. * If you need to assert a different exit code, then you can use
  266. * executeCommand() and perform a different assertion on the process object.
  267. */
  268. public function assertCommandSuccessful() {
  269. return $this->assertCommandExitCode(0);
  270. }
  271. /**
  272. * Asserts that the last command returned the specified exit code.
  273. *
  274. * @param int $expected_code
  275. * The expected process exit code.
  276. */
  277. public function assertCommandExitCode($expected_code) {
  278. $this->assertEquals($expected_code, $this->commandProcess->getExitCode(),
  279. 'COMMAND: ' . $this->commandProcess->getCommandLine() . "\n" .
  280. 'OUTPUT: ' . $this->commandProcess->getOutput() . "\n" .
  281. 'ERROR: ' . $this->commandProcess->getErrorOutput() . "\n"
  282. );
  283. }
  284. /**
  285. * Run a command.
  286. *
  287. * @param string $command_line
  288. * A command line to run in an isolated process.
  289. * @param string $working_dir
  290. * (optional) A working directory relative to the workspace, within which to
  291. * execute the command. Defaults to the workspace directory.
  292. *
  293. * @return \Symfony\Component\Process\Process
  294. */
  295. public function executeCommand($command_line, $working_dir = NULL) {
  296. $this->commandProcess = new Process($command_line);
  297. $this->commandProcess->setWorkingDirectory($this->getWorkingPath($working_dir))
  298. ->setTimeout(300)
  299. ->setIdleTimeout(300);
  300. $this->commandProcess->run();
  301. return $this->commandProcess;
  302. }
  303. /**
  304. * Helper function to assert that the last visit was a Drupal site.
  305. *
  306. * This method asserts that the X-Generator header shows that the site is a
  307. * Drupal site.
  308. */
  309. public function assertDrupalVisit() {
  310. $this->getMink()->assertSession()->responseHeaderMatches('X-Generator', '/Drupal \d+ \(https:\/\/www.drupal.org\)/');
  311. }
  312. /**
  313. * Visit a URI on the HTTP server.
  314. *
  315. * The concept here is that there could be multiple potential docroots in the
  316. * workspace, so you can use whichever ones you want.
  317. *
  318. * @param string $request_uri
  319. * (optional) The non-host part of the URL. Example: /some/path?foo=bar.
  320. * Defaults to visiting the homepage.
  321. * @param string $working_dir
  322. * (optional) Relative path within the test workspace file system that will
  323. * be the docroot for the request. Defaults to the workspace directory.
  324. *
  325. * @return \Behat\Mink\Mink
  326. * The Mink object. Perform assertions against this.
  327. *
  328. * @throws \InvalidArgumentException
  329. * Thrown when $request_uri does not start with a slash.
  330. */
  331. public function visit($request_uri = '/', $working_dir = NULL) {
  332. if ($request_uri[0] !== '/') {
  333. throw new \InvalidArgumentException('URI: ' . $request_uri . ' must be relative. Example: /some/path?foo=bar');
  334. }
  335. // Try to make a server.
  336. $this->standUpServer($working_dir);
  337. $request = 'http://localhost:' . $this->getPortNumber() . $request_uri;
  338. $this->mink->getSession()->visit($request);
  339. return $this->mink;
  340. }
  341. /**
  342. * Makes a local test server using PHP's internal HTTP server.
  343. *
  344. * Test authors should call visit() or assertVisit() instead.
  345. *
  346. * @param string|null $working_dir
  347. * (optional) Server docroot relative to the workspace file system. Defaults
  348. * to the workspace directory.
  349. */
  350. protected function standUpServer($working_dir = NULL) {
  351. // If the user wants to test a new docroot, we have to shut down the old
  352. // server process and generate a new port number.
  353. if ($working_dir !== $this->serverDocroot && !empty($this->serverProcess)) {
  354. $this->stopServer();
  355. }
  356. // If there's not a server at this point, make one.
  357. if (!$this->serverProcess || $this->serverProcess->isTerminated()) {
  358. $this->serverProcess = $this->instantiateServer($this->getPortNumber(), $working_dir);
  359. if ($this->serverProcess) {
  360. $this->serverDocroot = $working_dir;
  361. }
  362. }
  363. }
  364. /**
  365. * Do the work of making a server process.
  366. *
  367. * Test authors should call visit() or assertVisit() instead.
  368. *
  369. * When initializing the server, if '.ht.router.php' exists in the root, it is
  370. * leveraged. If testing with a version of Drupal before 8.5.x., this file
  371. * does not exist.
  372. *
  373. * @param int $port
  374. * The port number for the server.
  375. * @param string|null $working_dir
  376. * (optional) Server docroot relative to the workspace filesystem. Defaults
  377. * to the workspace directory.
  378. *
  379. * @return \Symfony\Component\Process\Process
  380. * The server process.
  381. *
  382. * @throws \RuntimeException
  383. * Thrown if we were unable to start a web server.
  384. */
  385. protected function instantiateServer($port, $working_dir = NULL) {
  386. $finder = new PhpExecutableFinder();
  387. $working_path = $this->getWorkingPath($working_dir);
  388. $server = [
  389. $finder->find(),
  390. '-S',
  391. self::$hostName . ':' . $port,
  392. '-t',
  393. $working_path,
  394. ];
  395. if (file_exists($working_path . DIRECTORY_SEPARATOR . '.ht.router.php')) {
  396. $server[] = $working_path . DIRECTORY_SEPARATOR . '.ht.router.php';
  397. }
  398. $ps = new Process($server, $working_path);
  399. $ps->setIdleTimeout(30)
  400. ->setTimeout(30)
  401. ->start();
  402. // Wait until the web server has started. It is started if the port is no
  403. // longer available.
  404. for ($i = 0; $i < 1000; $i++) {
  405. if (!$this->checkPortIsAvailable($port)) {
  406. return $ps;
  407. }
  408. usleep(1000);
  409. }
  410. throw new \RuntimeException(sprintf("Unable to start the web server.\nERROR OUTPUT:\n%s", $ps->getErrorOutput()));
  411. }
  412. /**
  413. * Stop the HTTP server, zero out all necessary variables.
  414. */
  415. protected function stopServer() {
  416. if (!empty($this->serverProcess)) {
  417. $this->serverProcess->stop();
  418. }
  419. $this->serverProcess = NULL;
  420. $this->serverDocroot = NULL;
  421. $this->hostPort = NULL;
  422. $this->initMink();
  423. }
  424. /**
  425. * Discover an available port number.
  426. *
  427. * @return int
  428. * The available port number that we discovered.
  429. *
  430. * @throws \RuntimeException
  431. * Thrown when there are no available ports within the range.
  432. */
  433. protected function findAvailablePort() {
  434. $store = new FlockStore(DrupalFilesystem::getOsTemporaryDirectory());
  435. $lock_factory = new Factory($store);
  436. $counter = 100;
  437. while ($counter--) {
  438. // Limit to 9999 as higher ports cause random fails on DrupalCI.
  439. $port = random_int(1024, 9999);
  440. if (isset($this->portLocks[$port])) {
  441. continue;
  442. }
  443. // Take a lock so that no other process can use the same port number even
  444. // if the server is yet to start.
  445. $lock = $lock_factory->createLock('drupal-build-test-port-' . $port);
  446. if ($lock->acquire()) {
  447. if ($this->checkPortIsAvailable($port)) {
  448. $this->portLocks[$port] = $lock;
  449. return $port;
  450. }
  451. else {
  452. $lock->release();
  453. }
  454. }
  455. }
  456. throw new \RuntimeException('Unable to find a port available to run the web server.');
  457. }
  458. /**
  459. * Checks whether a port is available.
  460. *
  461. * @param $port
  462. * A number between 1024 and 65536.
  463. *
  464. * @return bool
  465. */
  466. protected function checkPortIsAvailable($port) {
  467. $fp = @fsockopen(self::$hostName, $port, $errno, $errstr, 1);
  468. // If fsockopen() fails to connect, probably nothing is listening.
  469. // It could be a firewall but that's impossible to detect, so as a
  470. // best guess let's return it as available.
  471. if ($fp === FALSE) {
  472. return TRUE;
  473. }
  474. else {
  475. fclose($fp);
  476. }
  477. return FALSE;
  478. }
  479. /**
  480. * Get the port number for requests.
  481. *
  482. * Test should never call this. Used by standUpServer().
  483. *
  484. * @return int
  485. */
  486. protected function getPortNumber() {
  487. if (empty($this->hostPort)) {
  488. $this->hostPort = $this->findAvailablePort();
  489. }
  490. return $this->hostPort;
  491. }
  492. /**
  493. * Copy the current working codebase into a workspace.
  494. *
  495. * Use this method to copy the current codebase, including any patched
  496. * changes, into the workspace.
  497. *
  498. * By default, the copy will exclude sites/default/settings.php,
  499. * sites/default/files, and vendor/. Use the $iterator parameter to override
  500. * this behavior.
  501. *
  502. * @param \Iterator|null $iterator
  503. * (optional) An iterator of all the files to copy. Default behavior is to
  504. * exclude site-specific directories and files.
  505. * @param string|null $working_dir
  506. * (optional) Relative path within the test workspace file system that will
  507. * contain the copy of the codebase. Defaults to the workspace directory.
  508. */
  509. public function copyCodebase(\Iterator $iterator = NULL, $working_dir = NULL) {
  510. $working_path = $this->getWorkingPath($working_dir);
  511. if ($iterator === NULL) {
  512. $iterator = $this->getCodebaseFinder()->getIterator();
  513. }
  514. $fs = new SymfonyFilesystem();
  515. $options = ['override' => TRUE, 'delete' => FALSE];
  516. $fs->mirror($this->getDrupalRoot(), $working_path, $iterator, $options);
  517. }
  518. /**
  519. * Get a default Finder object for a Drupal codebase.
  520. *
  521. * This method can be used two ways:
  522. * - Override this method and provide your own default Finder object for
  523. * copyCodebase().
  524. * - Call the method to get a default Finder object which can then be
  525. * modified for other purposes.
  526. *
  527. * @return \Symfony\Component\Finder\Finder
  528. * A Finder object ready to iterate over core codebase.
  529. */
  530. public function getCodebaseFinder() {
  531. $finder = new Finder();
  532. $finder->files()
  533. ->ignoreUnreadableDirs()
  534. ->in($this->getDrupalRoot())
  535. ->notPath('#^sites/default/files#')
  536. ->notPath('#^sites/simpletest#')
  537. ->notPath('#^vendor#')
  538. ->notPath('#^sites/default/settings\..*php#')
  539. ->ignoreDotFiles(FALSE)
  540. ->ignoreVCS(FALSE);
  541. return $finder;
  542. }
  543. /**
  544. * Get the root path of this Drupal codebase.
  545. *
  546. * @return string
  547. * The full path to the root of this Drupal codebase.
  548. */
  549. protected function getDrupalRoot() {
  550. return realpath(dirname(__DIR__, 5));
  551. }
  552. }