Skip to content

Commit 49ca854

Browse files
martin-call-learningMartin
andauthored
Clean-up and tests (#1)
* Test get feed method * Refactor plugin and documentation Co-authored-by: Martin <[email protected]>
1 parent d5219ae commit 49ca854

File tree

15 files changed

+568
-250
lines changed

15 files changed

+568
-250
lines changed

.github/workflows/main.yml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
name: Moodle Plugin CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-22.04
8+
9+
services:
10+
postgres:
11+
image: postgres:12
12+
env:
13+
POSTGRES_USER: 'postgres'
14+
POSTGRES_HOST_AUTH_METHOD: 'trust'
15+
ports:
16+
- 5432:5432
17+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3
18+
mariadb:
19+
image: mariadb:10
20+
env:
21+
MYSQL_USER: 'root'
22+
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
23+
MYSQL_CHARACTER_SET_SERVER: "utf8mb4"
24+
MYSQL_COLLATION_SERVER: "utf8mb4_unicode_ci"
25+
26+
ports:
27+
- 3306:3306
28+
options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 3
29+
30+
strategy:
31+
fail-fast: false
32+
matrix:
33+
php: [ '7.4', '8.0' ]
34+
moodle-branch: [ 'MOODLE_311_STABLE','MOODLE_400_STABLE' ]
35+
database: [ pgsql, mariadb ]
36+
37+
steps:
38+
- name: Check out repository code
39+
uses: actions/checkout@v2
40+
with:
41+
path: plugin
42+
43+
- name: Setup PHP ${{ matrix.php }}
44+
uses: shivammathur/setup-php@v2
45+
with:
46+
php-version: ${{ matrix.php }}
47+
extensions: ${{ matrix.extensions }}
48+
ini-values: max_input_vars=5000
49+
# none to use phpdbg fallback. Specify pcov (Moodle 3.10 and up) or xdebug to use them instead.
50+
coverage: none
51+
52+
- name: Initialise moodle-plugin-ci
53+
run: |
54+
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3
55+
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
56+
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
57+
sudo locale-gen en_AU.UTF-8
58+
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
59+
60+
- name: Install moodle-plugin-ci
61+
run: |
62+
moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1
63+
env:
64+
DB: ${{ matrix.database }}
65+
MOODLE_BRANCH: ${{ matrix.moodle-branch }}
66+
67+
- name: PHP Lint
68+
if: ${{ always() }}
69+
run: moodle-plugin-ci phplint
70+
71+
- name: PHP Copy/Paste Detector
72+
continue-on-error: true # This step will show errors but will not fail
73+
if: ${{ always() }}
74+
run: moodle-plugin-ci phpcpd
75+
76+
- name: PHP Mess Detector
77+
continue-on-error: true # This step will show errors but will not fail
78+
if: ${{ always() }}
79+
run: moodle-plugin-ci phpmd
80+
81+
- name: Moodle Code Checker
82+
if: ${{ always() }}
83+
run: moodle-plugin-ci codechecker --max-warnings 0
84+
85+
- name: Moodle PHPDoc Checker
86+
if: ${{ always() }}
87+
run: moodle-plugin-ci phpdoc
88+
89+
- name: Validating
90+
if: ${{ always() }}
91+
run: moodle-plugin-ci validate
92+
93+
- name: Check upgrade savepoints
94+
if: ${{ always() }}
95+
run: moodle-plugin-ci savepoints
96+
97+
- name: Mustache Lint
98+
if: ${{ always() }}
99+
run: moodle-plugin-ci mustache
100+
101+
- name: Grunt
102+
if: ${{ always() }}
103+
run: moodle-plugin-ci grunt --max-lint-warnings 0
104+
105+
- name: PHPUnit tests
106+
if: ${{ always() }}
107+
run: moodle-plugin-ci phpunit --fail-on-warning
108+
109+
- name: Behat features
110+
if: ${{ always() }}
111+
run: moodle-plugin-ci behat --profile chrome

amd/src/config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
22
* RSS Thumbnails block
33
*
4-
* @package block_rss_thumbnails
54
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
65
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
76
*/
8-
define(['core/config'], function (cfg) {
7+
define(['core/config'], function(cfg) {
98
window.requirejs.config({
109
paths: {
1110
"glide":

amd/src/glide.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/**
22
* RSS Thumbnails block
33
*
4-
* @package block_rss_thumbnails
54
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
65
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
76
*/
8-
define([ 'jquery', 'block_rss_thumbnails/config'], function ($) {
9-
return function (locator, config) {
10-
require(['glide'], function (Glide) {
7+
define(['jquery', 'block_rss_thumbnails/config'], function($) {
8+
return function(locator, config) {
9+
require(['glide'], function(Glide) {
1110
// Show the slider now we are initialised.
1211
$(locator).removeClass('d-none');
1312
new Glide(locator, config).mount();

block_rss_thumbnails.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

25+
use block_rss_client\output\channel_image;
26+
use block_rss_client\output\feed;
2527
use block_rss_thumbnails\output\block;
28+
use block_rss_thumbnails\output\item;
2629

2730
defined('MOODLE_INTERNAL') || die();
2831
global $CFG;
@@ -37,6 +40,7 @@
3740
*/
3841
class block_rss_thumbnails extends block_rss_client {
3942

43+
/** @var int The default caroussel speed */
4044
const DEFAULT_CAROUSSEL_SPEED = 4000;
4145

4246
/** @var bool track whether any of the output feeds have recorded failures */
@@ -54,7 +58,7 @@ public function init() {
5458
/**
5559
* Content for the block
5660
*
57-
* @return \stdClass|string|null
61+
* @return stdClass|string|null
5862
* @throws coding_exception
5963
*/
6064
public function get_content() {
@@ -67,7 +71,7 @@ public function get_content() {
6771
return $this->content;
6872
}
6973

70-
// initalise block content object
74+
// Initialise block content object.
7175
$this->content = new stdClass;
7276
$this->content->text = '';
7377
$this->content->footer = '';
@@ -78,7 +82,7 @@ public function get_content() {
7882

7983
if (!isset($this->config)) {
8084
// The block has yet to be configured - just display configure message in
81-
// the block if user has permission to configure it
85+
// the block if user has permission to configure it.
8286

8387
if (has_capability('block/rss_client:manageanyfeeds', $this->context)) {
8488
$this->content->text = get_string('feedsconfigurenewinstance2', 'block_rss_client');
@@ -138,7 +142,7 @@ public function get_content() {
138142
* @param array $feedrecords The feed records from the database.
139143
* @return block_rss_client\output\footer|null The renderable footer or null if none should be displayed.
140144
*/
141-
protected function get_footer($feedrecords) {
145+
protected function get_footer($feedrecords) : ?block_rss_client\output\footer {
142146
$footer = null;
143147

144148
if (!empty($this->config->show_channel_link)) {
@@ -171,12 +175,12 @@ protected function get_footer($feedrecords) {
171175
/**
172176
* Returns the html of a feed to be displaed in the block
173177
*
174-
* @param mixed feedrecord The feed record from the database
175-
* @param int maxentries The maximum number of entries to be displayed
176-
* @param boolean showtitle Should the feed title be displayed in html
178+
* @param mixed $feedrecord The feed record from the database
179+
* @param int $maxentries The maximum number of entries to be displayed
180+
* @param boolean $showtitle Should the feed title be displayed in html
177181
* @return block_rss_client\output\feed|null The renderable feed or null of there is an error
178182
*/
179-
public function get_feed($feedrecord, $maxentries, $showtitle) {
183+
public function get_feed($feedrecord, $maxentries, $showtitle) : ?feed {
180184
global $CFG;
181185
require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
182186

@@ -186,7 +190,15 @@ public function get_feed($feedrecord, $maxentries, $showtitle) {
186190
return null;
187191
}
188192

189-
$simplepiefeed = new moodle_simplepie($feedrecord->url);
193+
if (!empty($feedrecord->url)) {
194+
$simplepiefeed = new moodle_simplepie($feedrecord->url);
195+
} else {
196+
$simplepiefeed = new moodle_simplepie();
197+
$simplepiefeed->set_file($feedrecord->fileurl);
198+
$simplepiefeed->enable_cache(false);
199+
$simplepiefeed->init();
200+
201+
}
190202

191203
if (isset($CFG->block_rss_client_timeout)) {
192204
$simplepiefeed->set_cache_duration($CFG->block_rss_client_timeout * 60);
@@ -209,12 +221,12 @@ public function get_feed($feedrecord, $maxentries, $showtitle) {
209221
}
210222

211223
if (empty($this->config->title)) {
212-
//NOTE: this means the 'last feed' displayed wins the block title - but
213-
//this is exiting behaviour..
224+
// NOTE: this means the 'last feed' displayed wins the block title - but
225+
// this is exiting behaviour..
214226
$this->title = strip_tags($feedtitle);
215227
}
216228

217-
$feed = new \block_rss_client\output\feed($feedtitle, $showtitle, false);
229+
$feed = new feed($feedtitle, $showtitle, false);
218230

219231
if ($simplepieitems = $simplepiefeed->get_items(0, $maxentries)) {
220232
foreach ($simplepieitems as $simplepieitem) {
@@ -231,7 +243,7 @@ public function get_feed($feedrecord, $maxentries, $showtitle) {
231243
return $cat->term;
232244
}, $simplepieitem->get_categories());
233245

234-
$item = new \block_rss_thumbnails\output\item(
246+
$item = new item(
235247
$simplepieitem->get_id(),
236248
new moodle_url($simplepieitem->get_link()),
237249
$simplepieitem->get_title(),
@@ -247,7 +259,7 @@ public function get_feed($feedrecord, $maxentries, $showtitle) {
247259
} catch (moodle_exception $e) {
248260
// If there is an error with the RSS item, we don't
249261
// want to crash the page. Specifically, moodle_url can
250-
// throw an exception of the param is an extremely
262+
// throw an exception if the param is an extremely
251263
// malformed url.
252264
debugging($e->getMessage());
253265
}
@@ -257,15 +269,15 @@ public function get_feed($feedrecord, $maxentries, $showtitle) {
257269
// Feed image.
258270
if ($imageurl = $simplepiefeed->get_image_url()) {
259271
try {
260-
$image = new \block_rss_client\output\channel_image(
272+
$image = new channel_image(
261273
new moodle_url($imageurl),
262274
$simplepiefeed->get_image_title(),
263275
new moodle_url($simplepiefeed->get_image_link())
264276
);
265277

266278
$feed->set_image($image);
267279
} catch (moodle_exception $e) {
268-
// If there is an error with the RSS image, we don'twant to
280+
// If there is an error with the RSS image, we don't want to
269281
// crash the page. Specifically, moodle_url can throw an
270282
// exception if the param is an extremely malformed url.
271283
debugging($e->getMessage());

classes/output/block.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
*/
2424

2525
namespace block_rss_thumbnails\output;
26-
defined('MOODLE_INTERNAL') || die();
26+
27+
use renderer_base;
2728

2829
/**
2930
* Class to help display an RSS Feeds block
@@ -32,26 +33,28 @@
3233
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
3334
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3435
*/
35-
class block extends \block_rss_client\output\block {
36+
class block extends \block_rss_client\output\block {
3637

37-
protected $carousselspeed = 0;
38+
/** @var int The delay between two slides */
39+
protected int $carousselspeed = 0;
3840
/**
3941
* Contruct
4042
*
43+
* @param int $carousselspeed the caroussel speed of the block
4144
* @param array $feeds An array of renderable feeds
4245
*/
4346
public function __construct($carousselspeed, array $feeds = array()) {
44-
$this->feeds = $feeds;
47+
parent::__construct($feeds);
4548
$this->carousselspeed = $carousselspeed;
4649
}
4750

4851
/**
4952
* Prepare data for use in a template
5053
*
51-
* @param \renderer_base $output
54+
* @param renderer_base $output
5255
* @return array
5356
*/
54-
public function export_for_template(\renderer_base $output) {
57+
public function export_for_template(renderer_base $output): array {
5558
$data = parent::export_for_template($output);
5659
$data['carousselspeed'] = $this->carousselspeed;
5760
return $data;

0 commit comments

Comments
 (0)