Skip to content
Snippets Groups Projects
Commit 23255aa6 authored by Henok Mikre's avatar Henok Mikre Committed by Henok Mikre
Browse files

Issue #3346533 by henokmikre: Drupal 10 compatibility with automated testing

parent 58fb3d29
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
"homepage": "http://drupal.org/project/static_genertor", "homepage": "http://drupal.org/project/static_genertor",
"license": "GPL-2.0+", "license": "GPL-2.0+",
"require": { "require": {
"drupal/core": "^8 || ^9" "drupal/core": "^9.4 || ^10"
}, },
"extra": { "extra": {
"drush": { "drush": {
...@@ -14,4 +14,4 @@ ...@@ -14,4 +14,4 @@
} }
} }
} }
} }
\ No newline at end of file
#!/bin/sh
cd /var/www/html/web
# Delete
/var/www/html/vendor/bin/drupal sgd --pages
/var/www/html/vendor/bin/drupal sgd --esi
# Pages
/var/www/html/vendor/bin/drupal sgp /important-page1
/var/www/html/vendor/bin/drupal sgp /important-page2
/var/www/html/vendor/bin/drupal sgp /error-403
/var/www/html/vendor/bin/drupal sgp /error-404
# Node
/var/www/html/vendor/bin/drupal sgpt node bio 0 10000&
sleep 2
#/var/www/html/vendor/bin/drupal sgpt node blog 0 10000&
#sleep 2
#/var/www/html/vendor/bin/drupal sgpt node contact_info 0 10000&
sleep 2
/var/www/html/vendor/bin/drupal sgpt node event 0 10000&
sleep 2
/var/www/html/vendor/bin/drupal sgpt node gallery 0 10000&
sleep 2
/var/www/html/vendor/bin/drupal sgpt node major_landing 0 10000&
sleep 2
/var/www/html/vendor/bin/drupal sgpt node news_release 0 10000&
sleep 2
/var/www/html/vendor/bin/drupal sgpt node page 0 10000&
sleep 2
/var/www/html/vendor/bin/drupal sgpt node section_landing 0 10000&
sleep 2
# Media
/var/www/html/vendor/bin/drupal sgpt media remote_video 0 10000&
sleep 2
# Taxonomy
/var/www/html/vendor/bin/drupal sgp /admin/structure/taxonomy/manage/cities/cincinnati
/var/www/html/vendor/bin/drupal sgpt vocabulary topics 0 10000&
#!/bin/bash
STATIC_DIR="/var/html/static"
HOST=`hostname`
# Generate XML Sitemap
cp /var/www/html/web/sites/default/files/xmlsitemap/NXkjkscfaf0440PdSzaEEnEVgmaugggggL25KojD4e9aZwOM/1.xml $STATIC_DIR/sitemap.xml
# RSS feeds
FILE_NAME_EVENTS=$STATIC_DIR/events.xml
curl -k -o $FILE_NAME_EVENTS https://$HOST/events.xml
FILE_EVENTS=$(<$FILE_NAME_EVENTS)
echo "${FILE_EVENTS//$HOST/www.example.com}" > $FILE_NAME_EVENTS
mkdir -p $STATIC_DIR/rss
FILE_NAME_FAQS=$STATIC_DIR/rss/efs-web-faqs.xml
curl -k -o $FILE_NAME_FAQS https://$HOST/rss/efs-web-faqs.xml
FILE_FAQS=$(<$FILE_NAME_FAQS)
echo "${FILE_FAQS//$HOST/www.example.com}" > $FILE_NAME_FAQS
mkdir -p $STATIC_DIR/news-updates
FILE_NAME_PR=$STATIC_DIR/about-us/news-updates/press-releases.xml
curl -k -o $FILE_NAME_PR https://$HOST/about-us/news-updates/press-releases.xml
FILE_PR=$(<$FILE_NAME_PR)
echo "${FILE_PR//$HOST/www.example.com}" > $FILE_NAME_PR
FILE_NAME_1=$STATIC_DIR/rss.xml
curl -k -o $FILE_NAME_1 https://$HOST/rss.xml
FILE_1=$(<$FILE_NAME_1)
echo "${FILE_1//https:\/\/example.com\/%3Ca%20href%3D%22https%3A\/\/example.com/https://www.example.com}" > $FILE_NAME_1
FILE_1=$(<$FILE_NAME_1)
echo "${FILE_1//%22%3E%3C\/a%3E/}" > $FILE_NAME_1
FILE_1=$(<$FILE_NAME_1)
echo "${FILE_1//https:\/\/example.com\/rss10.xml/https://example.com/rss.xml}" > $FILE_NAME_1
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\static_generator\Event; namespace Drupal\static_generator\Event;
use Symfony\Component\EventDispatcher\Event; use Drupal\Component\EventDispatcher\Event;
/** /**
* Allows modules to modify the markup. * Allows modules to modify the markup.
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\static_generator\Event; namespace Drupal\static_generator\Event;
use Symfony\Component\EventDispatcher\Event; use Drupal\Component\EventDispatcher\Event;
/** /**
* Allows modules to modify the markup. * Allows modules to modify the markup.
......
...@@ -310,6 +310,7 @@ class StaticGenerator { ...@@ -310,6 +310,7 @@ class StaticGenerator {
$start_time = time(); $start_time = time();
$query = \Drupal::entityQuery('media'); $query = \Drupal::entityQuery('media');
$query->accessCheck(TRUE);
$query->condition('status', 1); $query->condition('status', 1);
$query->condition('bundle', $bundle); $query->condition('bundle', $bundle);
$count = $query->count()->execute(); $count = $query->count()->execute();
...@@ -318,6 +319,7 @@ class StaticGenerator { ...@@ -318,6 +319,7 @@ class StaticGenerator {
for ($i = $start; $i <= $count; $i = $i + $length) { for ($i = $start; $i <= $count; $i = $i + $length) {
$query = \Drupal::entityQuery('media'); $query = \Drupal::entityQuery('media');
$query->accessCheck(TRUE);
$query->condition('status', 1); $query->condition('status', 1);
$query->condition('bundle', $bundle); $query->condition('bundle', $bundle);
$query->range($i, $length); $query->range($i, $length);
...@@ -413,6 +415,7 @@ class StaticGenerator { ...@@ -413,6 +415,7 @@ class StaticGenerator {
$start_time = time(); $start_time = time();
$query = \Drupal::entityQuery('taxonomy_term'); $query = \Drupal::entityQuery('taxonomy_term');
$query->accessCheck(TRUE);
$query->condition('status', 1); $query->condition('status', 1);
$query->condition('vid', $vid); $query->condition('vid', $vid);
$count = $query->count()->execute(); $count = $query->count()->execute();
...@@ -432,6 +435,7 @@ class StaticGenerator { ...@@ -432,6 +435,7 @@ class StaticGenerator {
// @TODO Can we reset container? // @TODO Can we reset container?
$query = \Drupal::entityQuery('taxonomy_term'); $query = \Drupal::entityQuery('taxonomy_term');
$query->accessCheck(TRUE);
$query->condition('status', 1); $query->condition('status', 1);
$query->condition('vid', $vid); $query->condition('vid', $vid);
$query->sort('weight'); $query->sort('weight');
...@@ -534,6 +538,7 @@ class StaticGenerator { ...@@ -534,6 +538,7 @@ class StaticGenerator {
$start_time = time(); $start_time = time();
$query = \Drupal::entityQuery('node'); $query = \Drupal::entityQuery('node');
$query->accessCheck(TRUE);
$query->condition('status', 1); $query->condition('status', 1);
$query->condition('type', $bundle); $query->condition('type', $bundle);
$count = $query->count()->execute(); $count = $query->count()->execute();
...@@ -541,6 +546,7 @@ class StaticGenerator { ...@@ -541,6 +546,7 @@ class StaticGenerator {
$count_gen = 0; $count_gen = 0;
$query = \Drupal::entityQuery('node'); $query = \Drupal::entityQuery('node');
$query->accessCheck(TRUE);
$query->condition('status', 1); $query->condition('status', 1);
$query->condition('type', $bundle); $query->condition('type', $bundle);
$query->range($start, $length); $query->range($start, $length);
...@@ -745,7 +751,7 @@ class StaticGenerator { ...@@ -745,7 +751,7 @@ class StaticGenerator {
// Allow modules to modify the markup (for nodes only). // Allow modules to modify the markup (for nodes only).
if ($node instanceof NodeInterface) { if ($node instanceof NodeInterface) {
$event = new ModifyMarkupEvent($markup, $node); $event = new ModifyMarkupEvent($markup, $node);
$this->eventDispatcher->dispatch(StaticGeneratorEvents::MODIFY_MARKUP, $event); $this->eventDispatcher->dispatch(new ModifyMarkupEvent($markup, $node), StaticGeneratorEvents::MODIFY_MARKUP);
$markup = $event->getMarkup(); $markup = $event->getMarkup();
} }
...@@ -1283,7 +1289,9 @@ class StaticGenerator { ...@@ -1283,7 +1289,9 @@ class StaticGenerator {
} }
if ($fid > 0) { if ($fid > 0) {
if ($draft) { if ($draft) {
$uri = \Drupal::database()->query('SELECT uri FROM file_managed WHERE fid=:fid', [':fid' => $fid])->fetchField(); $uri = \Drupal::database()
->query('SELECT uri FROM file_managed WHERE fid=:fid', [':fid' => $fid])
->fetchField();
if (!empty($uri)) { if (!empty($uri)) {
$exclude_file = substr($uri, 9); $exclude_file = substr($uri, 9);
$exclude_files .= '- ' . $exclude_file . "\r\n"; $exclude_files .= '- ' . $exclude_file . "\r\n";
...@@ -1389,6 +1397,7 @@ class StaticGenerator { ...@@ -1389,6 +1397,7 @@ class StaticGenerator {
->condition('moderation_state', 'draft') ->condition('moderation_state', 'draft')
->condition('moderation_state', 'scheduled_publish'); ->condition('moderation_state', 'scheduled_publish');
$q->latestRevision(); $q->latestRevision();
$q->accessCheck(TRUE);
$q->condition($or_cond); $q->condition($or_cond);
$draft_ids = $q->execute(); $draft_ids = $q->execute();
...@@ -1662,7 +1671,7 @@ class StaticGenerator { ...@@ -1662,7 +1671,7 @@ class StaticGenerator {
return ''; return '';
} }
} else { } else {
// Guzzle request using Drupal core (much slower than internal request). // Guzzle request.
$client = \Drupal::httpClient(['SERVER_NAME' => $static_url]); $client = \Drupal::httpClient(['SERVER_NAME' => $static_url]);
try { try {
$guzzle_host = $this->configFactory->get('static_generator.settings') $guzzle_host = $this->configFactory->get('static_generator.settings')
...@@ -2413,6 +2422,7 @@ class StaticGenerator { ...@@ -2413,6 +2422,7 @@ class StaticGenerator {
*/ */
public function excludeMediaIdsUnpublished() { public function excludeMediaIdsUnpublished() {
$query = \Drupal::entityQuery('media'); $query = \Drupal::entityQuery('media');
$query->accessCheck(TRUE);
$query->latestRevision(); $query->latestRevision();
$exclude_media_ids = $query->execute(); $exclude_media_ids = $query->execute();
return $exclude_media_ids; return $exclude_media_ids;
......
...@@ -2,8 +2,7 @@ name: "Static Generator" ...@@ -2,8 +2,7 @@ name: "Static Generator"
type: module type: module
description: "Generates a static version of a Drupal site." description: "Generates a static version of a Drupal site."
package: "Static Generator" package: "Static Generator"
core: 8.x core_version_requirement: '^9.4 || ^10'
core_version_requirement: ^8 || ^9
configure: static_generator.settings configure: static_generator.settings
dependencies: dependencies:
- system - system
...@@ -16,4 +15,4 @@ dependencies: ...@@ -16,4 +15,4 @@ dependencies:
- options - options
- node - node
- workflows - workflows
- content_moderation - content_moderation
\ No newline at end of file
...@@ -5,10 +5,10 @@ ...@@ -5,10 +5,10 @@
* Generates a static version of a Drupal site. * Generates a static version of a Drupal site.
*/ */
use Drupal\user\Entity\User;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
use Drupal\user\Entity\User;
/** /**
* Implements hook_help(). * Implements hook_help().
...@@ -40,7 +40,7 @@ function static_generator_help($route_name, RouteMatchInterface $route_match) { ...@@ -40,7 +40,7 @@ function static_generator_help($route_name, RouteMatchInterface $route_match) {
* @throws \Drupal\Core\Entity\EntityMalformedException * @throws \Drupal\Core\Entity\EntityMalformedException
*/ */
function static_generator_entity_insert(EntityInterface $entity) { function static_generator_entity_insert(EntityInterface $entity) {
process_workflow($entity, 'insert'); _static_generator_process_entity($entity, 'insert');
} }
/** /**
...@@ -51,7 +51,7 @@ function static_generator_entity_insert(EntityInterface $entity) { ...@@ -51,7 +51,7 @@ function static_generator_entity_insert(EntityInterface $entity) {
* @throws \Drupal\Core\Entity\EntityMalformedException * @throws \Drupal\Core\Entity\EntityMalformedException
*/ */
function static_generator_entity_update(EntityInterface $entity) { function static_generator_entity_update(EntityInterface $entity) {
process_workflow($entity, 'update'); _static_generator_process_entity($entity, 'update');
} }
/** /**
...@@ -63,7 +63,7 @@ function static_generator_entity_update(EntityInterface $entity) { ...@@ -63,7 +63,7 @@ function static_generator_entity_update(EntityInterface $entity) {
* *
* @throws \Drupal\Core\Entity\EntityMalformedException * @throws \Drupal\Core\Entity\EntityMalformedException
*/ */
function process_workflow(EntityInterface $entity, $operation) { function _static_generator_process_entity(EntityInterface $entity, $operation) {
if ($entity->getEntityTypeId() === 'node') { if ($entity->getEntityTypeId() === 'node') {
// Get list of content types allowed to be generated. // Get list of content types allowed to be generated.
$bundles_string = \Drupal::service('config.factory') $bundles_string = \Drupal::service('config.factory')
...@@ -188,7 +188,7 @@ function static_generator_form_alter(&$form, $form_state, $form_id) { ...@@ -188,7 +188,7 @@ function static_generator_form_alter(&$form, $form_state, $form_id) {
$has_generate_permission = User::load(\Drupal::currentUser() $has_generate_permission = User::load(\Drupal::currentUser()
->id())->hasPermission('generate static pages'); ->id())->hasPermission('generate static pages');
if ($has_generate_permission && substr($form_id,0,5) == 'node_' && \Drupal::service('static_generator')->endsWith($form_id,'_edit_form')) { if ($has_generate_permission && substr($form_id, 0, 5) == 'node_' && \Drupal::service('static_generator')->endsWith($form_id, '_edit_form')) {
$node = $form_state->getFormObject()->getEntity(); $node = $form_state->getFormObject()->getEntity();
if ($node instanceof NodeInterface) { if ($node instanceof NodeInterface) {
if ($node->isPublished()) { if ($node->isPublished()) {
...@@ -199,9 +199,9 @@ function static_generator_form_alter(&$form, $form_state, $form_id) { ...@@ -199,9 +199,9 @@ function static_generator_form_alter(&$form, $form_state, $form_id) {
} }
if ($has_generate_permission && in_array($form_id, [ if ($has_generate_permission && in_array($form_id, [
'media_image_edit_form', 'media_image_edit_form',
'media_remote_video_edit_form', 'media_remote_video_edit_form',
])) { ])) {
$media = $form_state->getFormObject()->getEntity(); $media = $form_state->getFormObject()->getEntity();
if ($media->isPublished()) { if ($media->isPublished()) {
\Drupal::service('static_generator') \Drupal::service('static_generator')
......
<?php
namespace Drupal\Tests\static_generator\Functional;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Tests\BrowserTestBase;
Use Drupal\Tests\block\Traits\BlockCreationTrait;
/**
* Tests generating blocks.
*
* @group block
*/
class StaticGeneratorBlockTest extends BrowserTestBase {
use BlockCreationTrait;
/**
* Admin user.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* Installation profile.
*
* @var string
*/
protected $profile = 'standard';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'static_generator',
];
/**
* Permissions to grant admin user.
*
* @var array
*/
protected $permissionsAdmin = [
'administer static generator',
'access administration pages',
'administer users',
'administer account settings',
'administer site configuration',
'administer user fields',
'administer user form display',
'administer user display',
'administer blocks',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser($this->permissionsAdmin, 'test_admin', TRUE);
$this->container->get('theme_installer')->install(['stable', 'classy']);
$this->container->get('config.factory')
->getEditable('system.theme')
->set('default', 'classy')
->save();
}
/**
* Tests block generation.
*
* @throws \Exception
*/
public function testBlockGeneration() {
// Login as admin user.
$this->drupalLogin($this->adminUser);
for ($i = 0; $i < 5000; $i++) {
// $block_id = substr(uniqid(), 0, 10);
// $default_theme = $this->config('system.theme')->get('default');
// $edit = [
// 'edit-info-0-value' => $block_id,
// ];
// $this->drupalGet('block/add');
// $this->assertSession()->pageTextContains('Add custom block');
// $this->drupalPostForm(NULL, $edit, t('Save'));
$this->placeBlock('system_powered_by_block');
}
// $this->drupalGet('/node');
// $this->assertSession()->statusCodeEquals(200);
// $this->assertSession()->responseContains('Powered by');
// $this->assertSession()
// ->elementsCount('css', '.block-system-powered-by-block', 2);
// $directory = 'private://';
// file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
$elapsed_time = \Drupal::service('static_generator')->generateBlocks();
$this->assertTrue($elapsed_time > 0, 'Block generation elapsed time: ' . $elapsed_time);
\Drupal::service('file_system')->saveData($elapsed_time, 'private://elapsed_time.txt', FileSystemInterface::EXISTS_REPLACE);
//drupal_unlink('private://index.html');
}
/**
* {@inheritdoc}
*/
protected function tearDown() {
}
}
\ No newline at end of file
...@@ -2,79 +2,38 @@ ...@@ -2,79 +2,38 @@
namespace Drupal\Tests\static_generator\Functional; namespace Drupal\Tests\static_generator\Functional;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\Routing\Route;
/** /**
* Verifies operation of the Static Generator service. * Tests that Static Generator works correctly.
* *
* @group static_generator * @group static_generator
*/ */
class StaticGeneratorTest extends BrowserTestBase { class StaticGeneratorTest extends StaticGeneratorTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'static_generator',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'bartik';
/**
* Permissions to grant admin user.
*
* @var array
*/
protected $permissionsAdmin = [
'administer static generator',
'access administration pages',
'administer users',
'administer account settings',
'administer site configuration',
'administer user fields',
'administer user form display',
'administer user display',
];
/**
* {@inheritdoc}
*/
public function setup() {
parent::setup();
// Create a page node.
$this->drupalCreateNode(['type' => 'page', 'title' => 'Test']);
}
/**
* Tests static generator caching of route.
*
* @param Route $route
* The route to cache.
*/
public function testCacheRoute() {
$this->assertTrue(TRUE);
}
/**
* Tests clearing the static generation cache.
*/
public function testCacheClear() {
$this->assertTrue(TRUE);
}
/** /**
* Tests generating markup for a single route. * Tests generating markup for a single route.
*/ */
public function testGenerateStaticMarkupForRoute() { public function testGenerateStaticMarkupForRoute() {
$static_markup = \Drupal::service('static_generator')->markupForPage('/node/1'); $session = $this->assertSession();
$this->assertContains('html', $static_markup); $session->statusCodeEquals(200);
// Create a test node.
$node = $this->drupalCreateNode([
'title' => 'Hello, world!',
'type' => 'article',
'body' => [
'value' => 'Test body text'
],
]);
// Check that the new values are found in the response.
$this->drupalGet('node/' . $node->id());
$session->statusCodeEquals(200);
// Generate the markup for the page.
$output = \Drupal::service('static_generator')->markupForPage('/node/' . $node->id());
// Assert that the generated markup contains the expected text.
$this->assertStringContainsString("Test body text", $output);
} }
} }
<?php
namespace Drupal\Tests\static_generator\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Base class to test StaticGenerator features.
*/
abstract class StaticGeneratorTestBase extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
// Core modules.
// @see testAvailableConfigEntities
'node',
// 'views', // Required to load the front page.
// // Core test modules.
// 'entity_test',
// 'test_page_test',
// This module.
'static_generator',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setup(): void {
parent::setup();
// Create content types.
if ($this->profile != 'standard') {
$this->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
'display_submitted' => FALSE,
]);
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
}
// Set configuration values for the Static Generator module.
$config = $this->config('static_generator.settings');
$config->set('guzzle_host', $this->getURL());
$config->set('static_url', 'http://static-site.local');
$config->set('gen_node', 'page, article');
$config->save();
// Initiate sessions with user with appropriate permissions.
$permissions = [
'administer static generator',
'access administration pages',
'administer site configuration',
];
$account = $this->drupalCreateUser($permissions);
$this->drupalLogin($account);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment