Skip to content

Commit

Permalink
Create dev management scripts
Browse files Browse the repository at this point in the history
 - use 'script' directory to mirror script-to-rule-them-all elsewhere
 - shortcut for css updating
 - create-superuser script for use in development envs
 - shortcut for changing cookie-domain in codespaces
  • Loading branch information
ajparsons committed Jun 6, 2023
1 parent 38c180d commit c885fa7
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 20 deletions.
5 changes: 2 additions & 3 deletions classes/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public static function output($template, $data = array(), $template_only = false
$data['footer_links'] = $footer->data;

# banner text
$b = new Model\Banner;
$data['banner_text'] = $b->get_text();

$announcement_manager = new Model\AnnouncementManagement;
$data['random_banner'] = $announcement_manager->get_random_valid_banner();
$data = self::addCommonURLs($data);

////////////////////////////////////////////////////////////
Expand Down
18 changes: 18 additions & 0 deletions script/codespaces-set-cookie-domain
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# This script is used to set the cookie domain when in codespaces
# rather than localhost, this will be something like ajparsons-fluffy-umbrella-wqwwwg9w7q3g694-80.preview.app.github.dev
# we can get the first bit from the CODESPACE_NAME environment variable

# if we're not in a codespace, then we don't need to do anything
if [ -z "$CODESPACES" ]; then
echo "Not in a codespace, skipping cookie domain setting"
exit 0
fi

CODESPACE_URL="$CODESPACE_NAME.preview.app.github.dev"

# we need to modify conf/general - where it currently says "localhost" we need to replace it with the above
sed -i "s/(\"COOKIEDOMAIN\", 'localhost')/(\"COOKIEDOMAIN\", 'github.dev')/g" conf/general

echo "Cookie domain set to github.dev"
117 changes: 117 additions & 0 deletions script/create-superuser
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/php

<?php

// create-superuser.php

// This script creates a superuser account in the database.
// It is intended to be be used to create a test superuser in dev

// Running the file will prompt for a username and password
// or these can be passed in with -u and -p arguments.

include_once dirname(__FILE__) . "/../www/includes/easyparliament/init.php";

// Parse command line arguments
$options = getopt("u:p:");

// Check if username and password were passed
if (isset($options["u"]) && isset($options["p"])) {
$email = $options["u"];
$password = $options["p"];
// if username isn't set, prompt for it
}
if (!isset($options["u"])) {
echo "Enter username (email address): ";
$email = trim(fgets(STDIN));
// if password isn't set, prompt for it
}

if (!isset($options["p"])) {
echo "Enter password: ";
system("stty -echo");
$password = trim(fgets(STDIN));
system("stty echo");
}

$details = [
"firstname" => "Test Admin",
"lastname" => "User",
"email" => $email,
"postcode" => "N13 4BS",
"url" => "https://mysociety.org",
"password" => $password,
"optin" => 1,
"status" => "Administrator",
"confirmed" => 1,
];

$registrationtime = gmdate("YmdHis");
$passwordforDB = password_hash($details["password"], PASSWORD_BCRYPT);

# get database connection
$db = new ParlDB();

$q = $db->query(
"INSERT INTO users (
firstname,
lastname,
email,
postcode,
url,
password,
optin,
status,
registrationtime,
registrationip,
facebook_id,
deleted,
confirmed
) VALUES (
:firstname,
:lastname,
:email,
:postcode,
:url,
:password,
:optin,
:status,
:registrationtime,
:registrationip,
:facebook_id,
'0',
:confirmed
)
",
[
":firstname" => $details["firstname"],
":lastname" => $details["lastname"],
":email" => $details["email"],
":postcode" => $details["postcode"],
":url" => $details["url"],
":password" => $passwordforDB,
":optin" => $details["optin"],
":status" => $details["status"],
":registrationtime" => $registrationtime,
":facebook_id" => "",
":registrationip" => "127.0.0.1",
":confirmed" => $details["confirmed"],
]
);

$q = $db->query("select * from users");

if ($q->success()) {
echo "\nUser created: Use the following details to log in:\n";
echo "Username: ";
echo $details["email"];
echo "\n";
echo "Password: ";
echo $details["password"];
echo "\n";
} else {
echo "Error creating user";
}


?>
4 changes: 4 additions & 0 deletions script/watch-css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

cd www/docs/style
bundle exec compass watch
23 changes: 19 additions & 4 deletions tests/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,35 @@ public function testSpeaker()
}

public function testBanner() {
$banner = new MySociety\TheyWorkForYou\Model\Banner;
$banner = new MySociety\TheyWorkForYou\Model\AnnouncementManagement;

# makes sure it is empty in case there's something hanging
# about in memcached
$banner->set_text('');
$banner->set_text('', "banner");
$page = $this->fetch_page( array( 'url' => '/' ) );
$this->assertNotContains('<div class="banner">', $page);
$this->assertNotContains('This is a banner', $page);

$banner->set_text('This is a banner');
$banner_config = '
[
{
"id":"basic-donate",
"content":"This is a banner",
"button_text":"Donate",
"button_link":"https://www.mysociety.org/donate/",
"button_class": "button--negative",
"weight":1,
"lang": "en",
"published":true
}
]
';

$banner->set_text($banner_config, "banner");
$page = $this->fetch_page( array( 'url' => '/' ) );
$this->assertContains('This is a banner', $page);

$banner->set_text('');
$banner->set_text('', "banner");
$page = $this->fetch_page( array( 'url' => '/' ) );
$this->assertNotContains('<div class="banner">', $page);
$this->assertNotContains('This is a banner', $page);
Expand Down
11 changes: 8 additions & 3 deletions www/docs/style/sass/layout/_header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,22 @@ $badge-horizontal-shadow-desktop: -8px;
color: #fff;

.banner__content {
font-size: 1.2em;
font-size: 1em;
line-height: 1.2em;
padding: 0.75em;
padding: 1em;
}

a {
a:not(.button) {
color: lighten($links, 5%);

&:hover,
&:focus {
color: lighten($primary-color, 5%);
}
}

a.button {
margin-bottom: 0;
margin-left: 0.5em;
}
}
23 changes: 13 additions & 10 deletions www/includes/easyparliament/templates/html/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,6 @@

<body>

<?php if ( $banner_text ) { ?>
<div class="banner">
<div class="full-page__row">
<div class="banner__content">
<?= $banner_text ?>
</div>
</div>
</div>
<?php } ?>

<?php if (isset($country) && in_array($country, array('AU', 'IE', 'CA'))) { ?>
<div class="banner">
<div class="full-page__row">
Expand Down Expand Up @@ -185,6 +175,19 @@
</div>
</nav>

<?php if (isset($random_banner) && $random_banner ) { ?>
<div class="banner">
<div class="full-page__row">
<div class="banner__content">
<?= $random_banner->content ?>
<?php if (isset($random_banner->button_link)) { ?>
<a class="button button--small content__button <?= $random_banner->button_class ?>" href="<?= $random_banner->button_link ?>"><?= $random_banner->button_text ?></a>
<?php } ?>
</div>
</div>
</div>
<?php } ?>

<?php if (isset($page_errors)) { ?>
<div class="full-page legacy-page static-page">
<div class="full-page__row">
Expand Down

0 comments on commit c885fa7

Please sign in to comment.