Commit 739d6f90 authored by Robert Lyon's avatar Robert Lyon Committed by Aaron Wells
Browse files

Bug 1605170: $view->get_artefact_instances() not working



It is meant to return artefact instances but was missing the plugin name

behatnotneeded: Testing with phpunit instead

Change-Id: Id12df696b932d3644e85fe6836a3d97910bf36ff
Signed-off-by: default avatarRobert Lyon <robertl@catalyst.net.nz>
parent cd9247b4
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -123,6 +123,42 @@ class ViewTest extends MaharaUnitTest {
        catch (Exception $e) {}
    }

    public function testView_get_artefact_instances() {
        // Empty view should return no artefact instances.
        $this->assertCount(0, $this->view->get_artefact_instances());

        for ($i = 0; $i < 3; $i++) {
            $bi = new BlockInstance(0,
                array(
                    'blocktype'  => 'textbox',
                    'title'      => 'test block ' . $i,
                    'view'       => $this->view->get('id'),
                    'view_obj'   => $this->view,
                    'row'        => $i,
                    'column'     => 0,
                    'order'      => 0,
                )
            );
            $bi->commit();
            $values = PluginBlocktypeTextbox::instance_config_save(
                array(
                    'text' => 'This is note ' . $i,
                    'tags' => null,
                    'artefactids' => null,
                ),
                $bi
            );
            $bi->set('configdata', $values);
            unset($values['_redrawblocks']);
            unset($values['title']);
            $bi->commit();
            $this->view->addblockinstance($bi);
            $this->view->commit();
        }

        $this->assertCount(3, $this->view->get_artefact_instances());
    }

    /**
     * clean up after ourselves,
     * just delete the test view we made
+18 −17
Original line number Diff line number Diff line
@@ -763,30 +763,31 @@ class View {
        $this->deleted = false;
    }

    /**
     * Returns an array of all the artefacts on this page.
     *
     * @return array
     */
    public function get_artefact_instances() {
        if (!isset($this->artefact_instances)) {
            $this->artefact_instances = false;
        $this->artefact_instances = array();
        if ($instances = $this->get_artefact_metadata()) {
            foreach ($instances as $instance) {
                safe_require('artefact', $instance->plugin);
                $classname = generate_artefact_class_name($instance->artefacttype);
                $i = new $classname($instance->id, $instance);
                    $this->childreninstances[] = $i;
                }
                $this->artefact_instances[] = $i;
            }
        }
        return $this->artefact_instances;
    }

    public function get_artefact_metadata() {
        if (!isset($this->artefact_metadata)) {
            $sql = 'SELECT a.*, i.name, va.block
        $sql = 'SELECT a.*, i.name, i.plugin, va.block
                FROM {view_artefact} va
                JOIN {artefact} a ON va.artefact = a.id
                JOIN {artefact_installed_type} i ON a.artefacttype = i.name
                WHERE va.view = ?';
        $this->artefact_metadata = get_records_sql_array($sql, array($this->id));
        }
        return $this->artefact_metadata;
    }