open-lms-open-source / moodle-theme_snap

GNU General Public License v3.0
79 stars 75 forks source link

Hard coded SQL that only works with mysql - fails unit test (test_service) and web service calls in postgres #186

Open kristian-94 opened 3 years ago

kristian-94 commented 3 years ago

This line of code: https://github.com/open-lms-open-source/moodle-theme_snap/blob/master/classes/webservice/ws_course_cards_categories.php#L174

Uses the mysql function FROM_UNIXTIME - https://www.w3resource.com/mysql/date-and-time-functions/mysql-from_unixtime-function.php

This won't work in moodles running postgres.

We also get the following unit test failure:

root@05fa61441cd5:/siteroot# vendor/bin/phpunit "theme_snap_ws_course_card_categories_test" theme/snap/tests/webservice_ws_course_card_categories_test.php Moodle 3.9.2+ (Build: 20200929), 47a9027d86d66847794f7c95c85d8374d1fb0d3a Php: 7.2.24.0.0.18.04.3, pgsql: 9.6.17, OS: Linux 4.15.0-117-generic x86_64 PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

..E 3 / 3 (100%)

Time: 1.51 seconds, Memory: 88.25 MB

There was 1 error:

1) theme_snap_ws_course_card_categories_test::test_service dml_read_exception: Error reading from database (ERROR: function from_unixtime(bigint) does not exist LINE 1: SELECT DISTINCT YEAR(FROM_UNIXTIME(c.enddate)) AS cat_year ,... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. SELECT DISTINCT YEAR(FROM_UNIXTIME(c.enddate)) AS cat_year , COUNT(*) AS courses, MIN(c.enddate) AS stamp FROM phpu_course c JOIN ( SELECT DISTINCT e.courseid FROM phpu_enrol e JOIN phpu_user_enrolments ue ON (ue.enrolid = e.id AND ue.userid = $1) WHERE ue.status = $2 AND e.status = $3 AND ue.timestart < $4 AND (ue.timeend = 0 OR ue.timeend > $5) UNION SELECT DISTINCT e.courseid FROM phpu_enrol e WHERE e.enrol = 'guest' AND e.password = $6 AND e.status = $7) en ON (en.courseid = c.id)

          LEFT JOIN phpu_context ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = $8)
          WHERE c.id <> $9
          GROUP BY cat_year

[array ( 0 => '196000', 1 => 0, 2 => 0, 3 => 1601439400.0, 4 => 1601439400.0, 5 => '', 6 => 0, 7 => 50, 8 => '1', )])

/siteroot/lib/dml/moodle_database.php:486 /siteroot/lib/dml/pgsql_native_moodle_database.php:329 /siteroot/lib/dml/pgsql_native_moodle_database.php:920 /siteroot/theme/snap/classes/webservice/ws_course_cards_categories.php:182 /siteroot/theme/snap/tests/webservice_ws_course_card_categories_test.php:86 /siteroot/lib/phpunit/classes/advanced_testcase.php:80

To re-run: vendor/bin/phpunit "theme_snap_ws_course_card_categories_test" theme/snap/tests/webservice_ws_course_card_categories_test.php

ERRORS! Tests: 3, Assertions: 2, Errors: 1.