Skip to content

Commit f169a8e

Browse files
committed
Issue #115: Fix failure to update cache before accessing it.
Move call to lib::reset_cms_names() to after calling config_on_update() in manage_content_types::update. Change cache key retrieval t o create a key if none exists. Previously would throw an exception Update unit tests.
1 parent 03552f8 commit f169a8e

9 files changed

+33
-16
lines changed

classes/local/datasource/base.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,10 @@ public function get_config_cache_key(): ?string {
391391
$key = $cmstype->get_custom_data(static::get_shortname() . 'confighash');
392392
// We expect there to be something, so false, null, '', and 0 are all illigit.
393393
if (empty($key)) {
394-
throw new \moodle_exception('error:no_config_hash', 'mod_cms', '', $this->cms->get('id'));
394+
debugging('Trying to gain a config cache key for ' . $this->cms->get('id') . ' without it being made.');
395+
$this->update_config_cache_key();
396+
$cmstype->read();
397+
$key = $cmstype->get_custom_data(static::get_shortname() . 'confighash');
395398
}
396399
return $key;
397400
}

classes/local/datasource/traits/hashcache.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ public function get_instance_cache_key(): ?string {
6262
$key = $this->cms->get_custom_data(self::get_shortname() . 'instancehash');
6363
// We expect there to be something, so false, null, '', and 0 are all illigit.
6464
if (empty($key)) {
65-
throw new \moodle_exception('error:no_instance_hash', 'mod_cms', '', $this->cms->get('id'));
65+
debugging('Trying to gain an instance cache key for ' . $this->cms->get('id') . ' without it being made.');
66+
$this->update_instance_cache_key();
67+
$key = $this->cms->get_custom_data(self::get_shortname() . 'instancehash');
6668
}
6769
}
6870
return $key;

classes/local/datasource/traits/revcache.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public function get_instance_cache_key(): ?string {
4949
$cacherev = $this->cms->get_custom_data($name);
5050
// We expect there to be something, so false, null, '', and 0 are all illigit.
5151
if (empty($cacherev)) {
52-
throw new \moodle_exception('error:no_instance_hash', 'mod_cms', '', $this->cms->get('id'));
52+
debugging('Trying to gain an instance cache key for ' . $this->cms->get('id') . ' without it being made.');
53+
$this->update_instance_cache_key();
54+
$cacherev = $this->cms->get_custom_data($name);
5355
}
5456
}
5557
// Combine with the ID to avoid clashes between instances.

classes/manage_content_types.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -363,16 +363,17 @@ public function create(\stdClass $data) {
363363
* @param \stdClass $data Form compatible data
364364
*/
365365
public function update(int $id, \stdClass $data) {
366-
$instance = $this->get_instance($id);
366+
$cmstype = $this->get_instance($id);
367367
$cleandata = cms_types::clean_record($data);
368-
$instance->from_record($cleandata);
369-
$instance->update();
370-
lib::reset_cms_names($id);
368+
$cmstype->from_record($cleandata);
369+
$cmstype->update();
371370

372371
// Do post update actions for data sources.
373-
foreach (dsbase::get_datasources($instance) as $ds) {
372+
foreach (dsbase::get_datasources($cmstype) as $ds) {
374373
$ds->config_on_update($data);
375374
}
375+
376+
lib::reset_cms_names($id);
376377
}
377378

378379
/**

tests/datasource_fields_test.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function test_no_hash() {
7070
$cmstype = new cms_types();
7171
$cmstype->set('name', 'name');
7272
$cmstype->set('idnumber', 'test-name');
73+
$cmstype->set('datasources', ['fields']);
7374
$cmstype->save();
7475

7576
$cms = $cmstype->get_sample_cms();
@@ -79,8 +80,9 @@ public function test_no_hash() {
7980
$cms->save();
8081

8182
$ds = new dsfields($cms);
82-
$this->expectException('moodle_exception');
83-
$ds->get_instance_cache_key();
83+
// A cache key should be generated if one does not already exist.
84+
$this->assertNotEmpty($ds->get_full_cache_key());
85+
$this->assertDebuggingCalledCount(2);
8486
}
8587

8688
/**

tests/datasource_images_test.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function test_no_hash() {
6666
$cmstype = new cms_types();
6767
$cmstype->set('name', 'name');
6868
$cmstype->set('idnumber', 'test-name');
69+
$cmstype->set('datasources', ['images']);
6970
$cmstype->save();
7071

7172
$cms = $cmstype->get_sample_cms();
@@ -76,10 +77,12 @@ public function test_no_hash() {
7677

7778
$ds = new dsimages($cms);
7879
$this->assertEquals('', $ds->get_instance_cache_key());
79-
$this->expectException('moodle_exception');
8080
$cmstype->set_custom_data('imagesconfighash', null);
8181
$cmstype->save();
8282
$ds->get_config_cache_key();
83+
// A cache key should be generated if one does not already exist.
84+
$this->assertNotEmpty($ds->get_full_cache_key());
85+
$this->assertDebuggingCalled();
8386
}
8487

8588
/**

tests/datasource_roles_test.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function test_no_hash() {
6767
$cmstype = new cms_types();
6868
$cmstype->set('name', 'name');
6969
$cmstype->set('idnumber', 'test-name');
70+
$cmstype->set('datasources', ['roles']);
7071
$cmstype->save();
7172

7273
$cms = $cmstype->get_sample_cms();
@@ -76,8 +77,9 @@ public function test_no_hash() {
7677
$cms->save();
7778

7879
$ds = new dsroles($cms);
79-
$this->expectException('moodle_exception');
80-
$ds->get_instance_cache_key();
80+
// A cache key should be generated if one does not already exist.
81+
$this->assertNotEmpty($ds->get_full_cache_key());
82+
$this->assertDebuggingCalledCount(2);
8183
}
8284

8385
/**

tests/datasource_userlist_test.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function test_no_hash() {
6363
$cmstype = new cms_types();
6464
$cmstype->set('name', 'name');
6565
$cmstype->set('idnumber', 'test-name');
66+
$cmstype->set('datasources', ['userlist']);
6667
$cmstype->save();
6768

6869
$cms = $cmstype->get_sample_cms();
@@ -72,8 +73,9 @@ public function test_no_hash() {
7273
$cms->save();
7374

7475
$ds = new dsuserlist($cms);
75-
$this->expectException('moodle_exception');
76-
$ds->get_instance_cache_key();
76+
// A cache key should be generated if one does not already exist.
77+
$this->assertNotEmpty($ds->get_full_cache_key());
78+
$this->assertDebuggingCalledCount(2);
7779
}
7880

7981
/**

version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
defined('MOODLE_INTERNAL') || die();
2727

28-
$plugin->version = 2023112900;
28+
$plugin->version = 2023120100;
2929
$plugin->requires = 2020061500; // Moodle 3.9.0 and above.
3030
$plugin->supported = [39, 401]; // Moodle 3.9 to 4.1 inclusive.
3131
$plugin->component = 'mod_cms';

0 commit comments

Comments
 (0)