Skip to content

Commit

Permalink
Fix validate command for table name prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
polothy committed Sep 29, 2017
1 parent 55acfe6 commit 526a6a0
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 3 deletions.
4 changes: 3 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
No unreleased changes.
### Fixed
- `moodle-plugin-ci validate` now properly validates all table name prefixes in the plugin's
`db/install.xml` file. Before, if any table name was properly prefixed, this would pass.

## [2.1.0] - 2017-09-13
### Fixed
Expand Down
10 changes: 10 additions & 0 deletions src/PluginValidate/Finder/FileTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,14 @@ public function hasFoundAllTokens()

return true;
}

/**
* Reset found state on all tokens.
*/
public function resetTokens()
{
foreach ($this->tokens as $token) {
$token->reset();
}
}
}
15 changes: 13 additions & 2 deletions src/PluginValidate/Finder/TablePrefixFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@ public function getType()

public function findTokens($file, FileTokens $fileTokens)
{
foreach ($this->findTables($file) as $table) {
$fileTokens->compareStart($table);
$tables = $this->findTables($file);
$total = count($tables);
for ($i = 0; $i < $total; ++$i) {
$fileTokens->compareStart($tables[$i]);

// This runs after every table except for the last one.
if ($i !== $total - 1) {
if (!$fileTokens->hasFoundAllTokens()) {
break; // Found an invalid table name, can stop.
}
// Current table name valid, reset tokens so we can see if the next table is valid or not.
$fileTokens->resetTokens();
}
}
}
}
8 changes: 8 additions & 0 deletions src/PluginValidate/Finder/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function __construct($token)
$this->tokens = $token;
}

/**
* Reset token found state.
*/
public function reset()
{
$this->found = false;
}

/**
* @return bool
*/
Expand Down
41 changes: 41 additions & 0 deletions tests/Fixture/bad-install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="local/travis/db" VERSION="2015071000" COMMENT="XMLDB file for Moodle local/travis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="local_travis_config" COMMENT="This is a config table.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="value" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="name" TYPE="unique" FIELDS="name"/>
</KEYS>
</TABLE>
<TABLE NAME="config" COMMENT="This is a config table.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="value" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="name" TYPE="unique" FIELDS="name"/>
</KEYS>
</TABLE>
<TABLE NAME="local_travis_config2" COMMENT="This is a config table.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="value" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="name" TYPE="unique" FIELDS="name"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
41 changes: 41 additions & 0 deletions tests/PluginValidate/Finder/TablePrefixFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Moodle Plugin CI package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Copyright (c) 2017 Blackboard Inc. (http://www.blackboard.com)
* License http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace Moodlerooms\MoodlePluginCI\Tests\PluginValidate\Finder;

use Moodlerooms\MoodlePluginCI\PluginValidate\Finder\FileTokens;
use Moodlerooms\MoodlePluginCI\PluginValidate\Finder\TablePrefixFinder;

class TablePrefixFinderTest extends \PHPUnit_Framework_TestCase
{
public function testFindTokens()
{
$file = __DIR__.'/../../Fixture/moodle-local_travis/db/install.xml';
$fileTokens = FileTokens::create('db/install.xml')->mustHave('local_travis');

$finder = new TablePrefixFinder();
$finder->findTokens($file, $fileTokens);

$this->assertTrue($fileTokens->hasFoundAllTokens());
}

public function testFindTokensFail()
{
$file = __DIR__.'/../../Fixture/bad-install.xml';
$fileTokens = FileTokens::create('db/install.xml')->mustHave('local_travis');

$finder = new TablePrefixFinder();
$finder->findTokens($file, $fileTokens);

$this->assertFalse($fileTokens->hasFoundAllTokens());
}
}

0 comments on commit 526a6a0

Please sign in to comment.