Skip to content

Commit

Permalink
Add WP CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
joppuyo committed Mar 24, 2024
1 parent f3c9ad6 commit f5e64df
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/Modules/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,26 @@ public function cli_init()

public function mangle()
{
\WP_CLI::success('Mangling media slugs');
$mangle = Mangle::get_instance();
\WP_CLI::line('Mangling media slugs...');
$attachment_ids = $mangle->get_attachments_to_mangle();
foreach ($attachment_ids as $attachment_id) {
\WP_CLI::line('Mangling attachment with ID ' . $attachment_id . ' and title ' . get_the_title($attachment_id));
$mangle->mangle_attachment($attachment_id);
}
\WP_CLI::success('Successfully mangled ' . count($attachment_ids) . ' media slugs!');
}

public function restore()
{
\WP_CLI::success('Restoring media slugs');
\WP_CLI::line('Restoring media slugs...');
$restore = Restore::get_instance();
$attachment_ids = $restore->get_attachments_to_restore();
foreach ($attachment_ids as $attachment_id) {
\WP_CLI::line('Restoring attachment with ID ' . $attachment_id . ' and title ' . get_the_title($attachment_id));
$restore->restore_attachment($attachment_id);
}
\WP_CLI::success('Successfully restored ' . count($attachment_ids) . ' media slugs!');
}

}
120 changes: 120 additions & 0 deletions tests/acceptance/CliMangleSlugsCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

use Facebook\WebDriver\Remote\RemoteWebDriver;

class CliMangleSlugsCest
{

public function iDoSetup(AcceptanceTester $I)
{
$I->importSqlDumpFile(codecept_data_dir('dump.sql'));

$I->cli(['option', 'update', 'home', $_ENV['TEST_SITE_WP_URL']]);
$I->cli(['option', 'update', 'siteurl', $_ENV['TEST_SITE_WP_URL']]);
$I->cli(['option', 'update', 'admin_email_lifespan', '2147483646']);

$I->cli(['core', 'update-db']);

try {
$I->cli(['config', 'set', 'AUTOMATIC_UPDATER_DISABLED', 'true', '--raw']);
$I->cli(['config', 'set', 'WP_DEBUG', 'true', '--raw']);
$I->cli(['config', 'set', 'SCRIPT_DEBUG', 'true', '--raw']);
} catch (Throwable $exception) {

}

$I->cli(['plugin', 'install', 'disable-welcome-messages-and-tips']);
$I->cli(['plugin', 'activate', 'disable-welcome-messages-and-tips']);

$I->cli(['theme', 'install', 'twentynineteen']);
$I->cli(['theme', 'activate', 'twentynineteen']);
}

public function iUploadImage(AcceptanceTester $I)
{
$I->loginAsAdmin();
$I->amOnAdminPage('upload.php');
$I->click('.page-title-action');
$I->attachFile(
'.moxie-shim input',
'example.jpg'
);
$I->waitForElementNotVisible('.attachment.uploading.save-ready');
$I->saveSessionSnapshot('login');
}

public function iGoToMediaPage(AcceptanceTester $I)
{
$I->loadSessionSnapshot('login');
$I->amOnPage('/example/');
$I->see('example');
}

public function iActivatePlugin(AcceptanceTester $I)
{
$I->loadSessionSnapshot('login');
$I->amOnPluginsPage();
$I->activatePlugin('disable-media-pages');
}

public function iGoToMediaPageAgain(AcceptanceTester $I)
{
$I->loadSessionSnapshot('login');
$I->amOnPage('/example/');
$I->dontSee('example');
$I->see('That page can’t be found.');
}

public function iMangleExistingAttachments(AcceptanceTester $I)
{
$I->cli(['disable-media-pages', 'mangle']);
}

public function createPage(AcceptanceTester $I)
{
global $wp_version;
$I->loadSessionSnapshot('login');
$I->amOnAdminPage('post-new.php?post_type=page');

if (version_compare($wp_version, '6.3', 'ge')) {
$I->switchToIFrame("editor-canvas");
// https://maslosoft.com/blog/2017/03/03/codeception-acceptance-filling-in-contenteditable/
$I->waitForElementVisible('.editor-post-title__input', 30);
$I->click('.editor-post-title__input');
$I->type('Example');
$I->switchToIFrame();
} else if (version_compare($wp_version, '6.2', 'ge')) {
// https://maslosoft.com/blog/2017/03/03/codeception-acceptance-filling-in-contenteditable/
$I->waitForElementVisible('.editor-post-title__input', 30);
$I->click('.editor-post-title__input');
$I->type('Example');
} else {
$I->fillField('.editor-post-title__input', 'Example');
}

$publish_text = 'Publish…';

if (version_compare($wp_version, '5.5', 'ge')) {
$publish_text = 'Publish';
}

$I->click($publish_text);

if (version_compare($wp_version, '5', 'ge')) {
$I->waitForElementVisible('.editor-post-publish-button', 30);
$I->click('.editor-post-publish-button');
}

$I->waitForText('Page published.', 60);
$I->amOnAdminPage('edit.php?post_type=page');
$I->see('Example');
}

public function iCheckPageUrl(AcceptanceTester $I)
{
$I->loadSessionSnapshot('login');
$I->amOnPage('/example/');
$I->see('Example');
}

}
109 changes: 109 additions & 0 deletions tests/acceptance/CliRestoreSlugsCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

class CliRestoreSlugsCest
{

public function iDoSetup(AcceptanceTester $I)
{
$I->importSqlDumpFile(codecept_data_dir('dump.sql'));

$I->cli(['option', 'update', 'home', $_ENV['TEST_SITE_WP_URL']]);
$I->cli(['option', 'update', 'siteurl', $_ENV['TEST_SITE_WP_URL']]);
$I->cli(['option', 'update', 'admin_email_lifespan', '2147483646']);

$I->cli(['core', 'update-db']);

try {
$I->cli(['config', 'set', 'AUTOMATIC_UPDATER_DISABLED', 'true', '--raw']);
} catch (Throwable $exception) {

}

$I->cli(['plugin', 'install', 'disable-welcome-messages-and-tips']);
$I->cli(['plugin', 'activate', 'disable-welcome-messages-and-tips']);

$I->cli(['theme', 'install', 'twentynineteen']);
$I->cli(['theme', 'activate', 'twentynineteen']);
}

public function iUploadImage(AcceptanceTester $I)
{
$I->loginAsAdmin();
$I->amOnAdminPage('upload.php');
$I->click('.page-title-action');
$I->attachFile(
'.moxie-shim input',
'example.jpg'
);
$I->waitForElementNotVisible('.attachment.uploading.save-ready');
$I->saveSessionSnapshot('login');
}

public function iGoToMediaPage(AcceptanceTester $I)
{
$I->loadSessionSnapshot('login');
$I->amOnPage('/example/');
$I->see('example');
}

public function iActivatePlugin(AcceptanceTester $I)
{
$I->loadSessionSnapshot('login');
$I->amOnPluginsPage();
$I->activatePlugin('disable-media-pages');
}

public function iGoToMediaPageAgain(AcceptanceTester $I)
{
$I->loadSessionSnapshot('login');
$I->amOnPage('/example/');
$I->dontSee('example');
$I->see('That page can’t be found.');
}

public function iMangleExistingAttachments(AcceptanceTester $I)
{
$I->loadSessionSnapshot('login');
$I->amOnAdminPage('options-general.php?page=disable-media-pages');
$I->waitForText('Mangle existing slugs');
$I->click('Mangle existing slugs');
$I->waitForText('Existing media slug mangling tool');
$I->click('Start mangling process');
$I->waitForText('All media slugs mangled');
}

public function iCheckSlugIsUuid(AcceptanceTester $I)
{
$query = new WP_Query([
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => 1,
]);

$post = $query->posts[0];

\PHPUnit\Framework\Assert::assertRegExp(
'/[0-9a-f]{8}[0-9a-f]{4}4[0-9a-f]{3}[89ab][0-9a-f]{3}[0-9a-f]{12}/', $post->post_name
);
}

public function iRestoreAttachmentSlugs(AcceptanceTester $I)
{
$I->cli(['disable-media-pages', 'restore']);
}

public function iDeactivatePlugin(AcceptanceTester $I)
{
$I->loadSessionSnapshot('login');
$I->amOnPluginsPage();
$I->deactivatePlugin('disable-media-pages');
}

public function iGoToMediaPageOnceMore(AcceptanceTester $I)
{
$I->loadSessionSnapshot('login');
$I->amOnPage('/example/');
$I->see('example');
}

}

0 comments on commit f5e64df

Please sign in to comment.