Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
Restore everything
Browse files Browse the repository at this point in the history
  • Loading branch information
phenaproxima committed Aug 2, 2024
1 parent 9a4ef31 commit 158bb2d
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 5 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@
"drupal:install": [
"Composer\\Config::disableProcessTimeout",
"\\Drupal\\starshot_installer\\ScriptHandler::configureDrush",
"drush site:install --yes --account-pass admin --config drush-install.yml standard",
"rm -f drush-install.yml"
"drush site:install --yes --account-pass admin --config drush-install.yml",
"rm -f drush-install.yml",
"drush webform-libraries-download"
],
"drupal:install-dev": [
"cd web/sites/default && chmod +w . && rm -rf settings.php files",
Expand Down
14 changes: 11 additions & 3 deletions tests/src/ExistingSite/BasicExpectationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ class BasicExpectationsTest extends ExistingSiteBase {
* Tests basic expectations of a successful Starshot install.
*/
public function testBasicExpectations(): void {
$this->drupalGet('/');

$assert_session = $this->assertSession();
$assert_session->statusCodeEquals(200);
$assert_session->elementAttributeContains('css', 'meta[name="Generator"]', 'content', 'Drupal');

// The installer should have uninstalled itself.
$this->assertFalse($this->container->getParameter('install_profile'));

// Ensure that there are non-core extensions installed, which proves that
// recipes were applied during site installation.
$this->assertContribInstalled($this->container->get(ModuleExtensionList::class));
Expand All @@ -31,9 +40,8 @@ public function testBasicExpectations(): void {
* An extension list.
*/
private function assertContribInstalled(ExtensionList $list): void {
foreach ($list->getAllInstalledInfo() as $info) {
// If this extension isn't part of core, great! We're done.
if ($info['lifecycle'] === 'stable' && $info['package'] !== 'Core') {
foreach ($list->getAllInstalledInfo() as ['package' => $package]) {
if (!str_starts_with($package, 'Core')) {
return;
}
}
Expand Down
119 changes: 119 additions & 0 deletions tests/src/ExistingSite/MetaTagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\starshot\ExistingSite;

use Behat\Mink\Element\NodeElement;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use weitzman\DrupalTestTraits\ExistingSiteBase;

/**
* @group starshot
*/
class MetaTagsTest extends ExistingSiteBase {

/**
* Tests the front page title.
*/
public function testFrontPageMetaTags(): void
{
// Create a random title expectation.
$expected_title = $this->getRandomGenerator()->name();

// Set the site name.
$config_factory = $this->container->get('config.factory');
$config_factory->getEditable('system.site')
->set('name', $expected_title)
->save();

// Get the front page title.
$this->drupalGet('<front>');
$title_tag = $this->getSession()
->getPage()
->find('xpath', '/head/title');
assert($title_tag instanceof NodeElement, 'Ensure that the "<title>" tag is found.');
$actual_title = $title_tag->getText();

$this->assertEquals($expected_title, $actual_title, 'Page title matches expected.');
}

/**
* @testWith ["page"]
* ["blog"]
* ["event", {"field_location": {"country_code": "US", "administrative_area": "DC", "locality": "Washington", "postal_code": 20560, "address_line1": "1000 Jefferson Dr SW"}}, {"og:country_name": "United States", "og:locality": "Washington", "og:region": "DC", "og:postal_code":"20560", "og:street_address": "1000 Jefferson Dr SW"}]
*/
public function testMetaTags(string $content_type, array $values = [], array $additional_meta_tags = []): void {
$random = $this->getRandomGenerator();

// If we create a page, all the expected meta tags should be there.
$node = $this->createNode($values + [
'type' => $content_type,
'body' => [
'summary' => 'Not a random summary...',
'value' => $random->paragraphs(1),
],
'moderation_state' => 'published',
]);
$this->drupalGet($node->toUrl());
$assert_session = $this->assertSession();
$assert_session->statusCodeEquals(200);
$this->assertMetaTag('description', 'Not a random summary...');
$this->assertMetaTag('og:description', 'Not a random summary...');
$this->assertMetaTag('og:title', $node->getTitle());
$this->assertMetaTag('og:type', $node->type->entity->label());

if ($node->hasField('field_image')) {
$file_uri = uniqid('public://') . '.png';
$file_uri = $random->image($file_uri, '100x100', '200x200');
$this->assertFileExists($file_uri);
$file = File::create(['uri' => $file_uri]);
$file->save();
$this->markEntityForCleanup($file);
$file_url = $this->container->get(FileUrlGeneratorInterface::class)
->generateAbsoluteString($file_uri);

$media = Media::create([
'name' => $random->word(16),
'bundle' => 'image',
'field_media_image' => [
'target_id' => $file->id(),
'alt' => 'Not random alt text...',
],
]);
$media->save();
$this->markEntityForCleanup($media);

$node->set('field_image', $media)->save();
$this->getSession()->reload();
$this->assertMetaTag('image_src', $file_url, 'link');
$this->assertMetaTag('og:image', $file_url);
$this->assertMetaTag('og:image:alt', 'Not random alt text...');
}

foreach ($additional_meta_tags as $name => $value) {
$this->assertMetaTag($name, $value);
}
}

private function assertMetaTag(string $name, string $expected_value, string $tag_name = 'meta', bool $exact_match = TRUE): void {
$name_attribute = match ($tag_name) {
'meta' => str_contains($name, ':') ? 'property' : 'name',
'link' => 'rel',
};

$actual_value = $this->assertSession()
->elementExists('css', $tag_name . '[' . $name_attribute . '="' . $name . '"]')
->getAttribute(match ($tag_name) { 'meta' => 'content', 'link' => 'href'});

if ($exact_match) {
$this->assertSame($expected_value, $actual_value);
}
else {
$this->assertStringContainsString($expected_value, $actual_value);
}
}

}

0 comments on commit 158bb2d

Please sign in to comment.