Skip to content

Commit

Permalink
Merge branch 'develop' into feature/129-ll-not-applied-lcp
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Jan 9, 2025
2 parents a24f6a2 + 1204467 commit 51576aa
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
10 changes: 10 additions & 0 deletions config/wp.config.sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import ScenarioUrls from "./scenarioUrls.json";
const WP_ADMIN_USER = {
username: 'live_username',
password: 'live_password',
username2: 'live_username2',
password2: 'live_password2',
localUsername: 'admin',
localPassword: 'password',
local: 'http://localhost',
Expand All @@ -31,6 +33,8 @@ const IMAGIFY_INFOS = {
* @type {{
* WP_USERNAME: string;
* WP_PASSWORD: string;
* WP_USERNAME2: string;
* WP_PASSWORD2: string;
* WP_BASE_URL: string;
* WP_ROOT_DIR: string;
* WP_ENV_TYPE: ServerType;
Expand All @@ -45,6 +49,8 @@ const IMAGIFY_INFOS = {
const {
WP_USERNAME = process.env.npm_config_env !== undefined ? WP_ADMIN_USER.localUsername : WP_ADMIN_USER.username,
WP_PASSWORD = process.env.npm_config_env !== undefined ? WP_ADMIN_USER.localPassword : WP_ADMIN_USER.password,
WP_USERNAME2 = process.env.npm_config_env !== undefined ? WP_ADMIN_USER.localUsername : WP_ADMIN_USER.username2,
WP_PASSWORD2 = process.env.npm_config_env !== undefined ? WP_ADMIN_USER.localPassword : WP_ADMIN_USER.password2,
WP_BASE_URL = process.env.npm_config_env !== undefined ? WP_ADMIN_USER.local : WP_ADMIN_USER.live,
WP_ROOT_DIR = '',
WP_ENV_TYPE = '',
Expand Down Expand Up @@ -77,6 +83,8 @@ const SCENARIO_URLS = ScenarioUrls[scriptName];
* @type {{
* WP_USERNAME: string;
* WP_PASSWORD: string;
* WP_USERNAME2: string;
* WP_PASSWORD2: string;
* WP_BASE_URL: string;
* WP_ROOT_DIR: string;
* WP_ENV_TYPE: ServerType;
Expand All @@ -97,6 +105,8 @@ const SCENARIO_URLS = ScenarioUrls[scriptName];
export {
WP_USERNAME,
WP_PASSWORD,
WP_USERNAME2,
WP_PASSWORD2,
WP_BASE_URL,
WP_ROOT_DIR,
WP_ENV_TYPE,
Expand Down
8 changes: 5 additions & 3 deletions src/support/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import { ChromiumBrowser, chromium } from '@playwright/test';
import { Sections } from '../common/sections';
import { selectors as pluginSelectors } from "./../common/selectors";
import { PageUtils } from "../../utils/page-utils";
import { deleteFolder } from "../../utils/helpers";
import { deleteFolder, isWprRelatedError } from "../../utils/helpers";
import {WP_SSH_ROOT_DIR,} from "../../config/wp.config";
import { After, AfterAll, Before, BeforeAll, Status, setDefaultTimeout } from "@cucumber/cucumber";
import {rename, exists, rm, testSshConnection, installRemotePlugin, activatePlugin, uninstallPlugin} from "../../utils/commands";
import {rename, exists, rm, testSshConnection, installRemotePlugin, activatePlugin, uninstallPlugin, readFile} from "../../utils/commands";
// import {configurations, getWPDir} from "../../utils/configurations";

/**
Expand Down Expand Up @@ -167,8 +167,10 @@ After(async function (this: ICustomWorld, { pickle, result }) {

const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug.log`;
const debugLogExists = await exists(debugLogPath);
const debugLogContents = await readFile(debugLogPath);
const wprRelatedError = await isWprRelatedError(debugLogContents);

if (debugLogExists && previousScenarioName) {
if (debugLogExists && previousScenarioName && wprRelatedError) {
// Close up white spaces.
previousScenarioName = previousScenarioName.toLowerCase();
previousScenarioName = previousScenarioName.replaceAll(' ', '-');
Expand Down
2 changes: 1 addition & 1 deletion src/support/steps/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ When('I go {string}', async function (this: ICustomWorld, url: string) {
*/
When('I connect as {string}', async function (this: ICustomWorld, user: string) {
await this.utils.wpAdminLogout();
await this.utils.auth();
await this.utils.auth('admin2');
});

/**
Expand Down
21 changes: 21 additions & 0 deletions utils/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,25 @@ export async function updatePostStatus(id: number, status: string): Promise<void
await wp(`post update ${id} --post_status=${status}`);
}

/**
* Read file on the server.
*
* @function
* @name readFile
* @async
* @param {string} path - The path to the file to be read.
* @returns {Promise<string>} - A Promise that resolves after file content is read.
*/
export async function readFile(path: string): Promise<string> {
const cwd = configurations.rootDir;
const command = wrapPrefix(`sudo cat ${path}`);
const result = exec(command, { cwd: cwd, async: false });

if (result.code !== 0) {
return '';
}

return result.stdout;
}

export default wp;
22 changes: 22 additions & 0 deletions utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,25 @@ export const getScenarioTag = async(tags: Array<string>): Promise<string> => {

return tag;
}

/**
* Check for WP Rocket related error in debug.log.
*
* @param {string} contents File content to be checked.
*
* @return {Promise<boolean>} Promise that resolves after check is completed.
*/
export const isWprRelatedError = async(contents: string): Promise<boolean> => {
const patterns: Array<string> = [
'/plugins/wp-rocket/',
'WP_Rocket'
];

for (const pattern of patterns) {
if (contents.includes(pattern)) {
return true;
}
}

return false;
}
17 changes: 10 additions & 7 deletions utils/page-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {expect} from "@playwright/test";
import { ICustomWorld } from '../src/common/custom-world';
import fs from "fs/promises";

import {WP_BASE_URL, WP_PASSWORD, WP_USERNAME} from '../config/wp.config';
import {WP_BASE_URL, WP_PASSWORD, WP_PASSWORD2, WP_USERNAME, WP_USERNAME2} from '../config/wp.config';
import { uninstallPlugin, updatePermalinkStructure, deactivatePlugin, switchTheme } from "./commands";

/**
Expand Down Expand Up @@ -75,12 +75,15 @@ export class PageUtils {
*
* @return {Promise<void>}
*/
public wpAdminLogin = async (): Promise<void> => {
public wpAdminLogin = async (user: string | null = null): Promise<void> => {
const username = user === 'admin2' ? WP_USERNAME2 : WP_USERNAME;
const password = user === 'admin2' ? WP_PASSWORD2 : WP_PASSWORD;

// Fill username & password.
await this.page.click('#user_login');
await this.page.fill('#user_login', WP_USERNAME);
await this.page.fill('#user_login', username);
await this.page.click('#user_pass');
await this.page.fill('#user_pass', WP_PASSWORD);
await this.page.fill('#user_pass', password);

// Click login.
await this.page.click('#wp-submit');
Expand Down Expand Up @@ -340,9 +343,10 @@ export class PageUtils {
/**
* Performs Wordpress login action.
*
* @param user - Optional username for login. If not provided, a default user will be used.
* @return {Promise<void>}
*/
public auth = async (): Promise<void> => {
public auth = async (user: string| null = null): Promise<void> => {
if(! this.page.url().includes('wp-login.php')) {
await this.visitPage('wp-admin');
}
Expand All @@ -351,8 +355,7 @@ export class PageUtils {
return ;
}

await this.wpAdminLogin();

await this.wpAdminLogin(user);
}

/**
Expand Down

0 comments on commit 51576aa

Please sign in to comment.