const METADATA_FILE = 'aff.json';
+ const LAYOUT_FILE = 'aff.html';
+
+ const FILE_REGEXP = '/\.aff\.(json|html)$/';
+
const DEFAULT_REQUIRES = 'afformCore';
/**
*/
public function getMeta($name) {
// FIXME error checking
- $metaFile = $this->findFilePath($name, self::METADATA_FILE);
- if (!$metaFile) {
- return NULL;
- }
$defaults = [
'name' => $name,
'is_public' => FALSE,
];
- return array_merge($defaults, json_decode(file_get_contents($metaFile), 1));
+ $metaFile = $this->findFilePath($name, self::METADATA_FILE);
+ if ($metaFile !== NULL) {
+ return array_merge($defaults, json_decode(file_get_contents($metaFile), 1));
+ }
+ elseif ($this->findFilePath($name, self::LAYOUT_FILE)) {
+ return $defaults;
+ }
+ else {
+ return NULL;
+ }
}
/**
* Lower priority files override higher priority files.
*/
private function appendFilePaths(&$formPaths, $parent, $priority) {
- $files = (array) glob("$parent/*." . self::METADATA_FILE);
+ $files = preg_grep(self::FILE_REGEXP, (array) glob("$parent/*"));
+
foreach ($files as $file) {
- $name = _afform_angular_module_name(preg_replace('/\.aff\.json$/', '', basename($file)));
- $formPaths[$name][$priority] = preg_replace('/\.aff\.json$/', '', $file);
+ $fileBase = preg_replace(self::FILE_REGEXP, '', $file);
+ $name = _afform_angular_module_name(basename($fileBase));
+ $formPaths[$name][$priority] = $fileBase;
ksort($formPaths[$name]);
}
}
return Civi::paths()->getPath('[civicrm.files]/ang');
}
+ /**
+ * @return string
+ */
+ private function getMarkerRegexp() {
+ static $v;
+ if ($v === NULL) {
+ $v = '/\.(' . preg_quote(self::LAYOUT_FILE, '/') . '|' . preg_quote(self::METADATA_FILE, '/') . ')$/';
+ }
+ return $v;
+ }
+
}
parent::tearDown();
}
+ public function getBasicDirectives() {
+ return [
+ ['afformExamplepage', ['title' => '', 'description' => '', 'server_route' => 'civicrm/example-page']],
+ ['fakelibBareFile', ['title' => '', 'description' => '']],
+ ['fakelibFoo', ['title' => '', 'description' => '']],
+ ];
+ }
+
/**
* This takes the bundled `examplepage` and performs some API calls on it.
+ * @dataProvider getBasicDirectives
*/
- public function testGetUpdateRevert() {
- Civi\Api4\Afform::revert()->addWhere('name', '=', 'afformExamplepage')->execute();
+ public function testGetUpdateRevert($directiveName, $originalMetadata) {
+ $get = function($arr, $key) {
+ return isset($arr[$key]) ? $arr[$key] : NULL;
+ };
+
+ Civi\Api4\Afform::revert()->addWhere('name', '=', $directiveName)->execute();
$message = 'The initial Afform.get should return default data';
- $result = Civi\Api4\Afform::get()->addWhere('name', '=', 'afformExamplepage')->execute();
- $result->indexBy('name');
- $b = (array) $result;
- $this->assertEquals('afformExamplepage', $result['afformExamplepage']['name'], $message);
- $this->assertEquals('', $result['afformExamplepage']['title'], $message);
- $this->assertEquals('', $result['afformExamplepage']['description'], $message);
- $this->assertEquals('civicrm/example-page', $result['afformExamplepage']['server_route'], $message);
- $this->assertTrue(is_array($result['afformExamplepage']['layout']), $message);
+ $result = Civi\Api4\Afform::get()->addWhere('name', '=', $directiveName)->execute();
+ $this->assertEquals($directiveName, $result[0]['name'], $message);
+ $this->assertEquals($get($originalMetadata, 'title'), $get($result[0], 'title'), $message);
+ $this->assertEquals($get($originalMetadata, 'description'), $get($result[0], 'description'), $message);
+ $this->assertEquals($get($originalMetadata, 'server_route'), $get($result[0], 'server_route'), $message);
+ $this->assertTrue(is_array($result[0]['layout']), $message);
$message = 'After updating with Afform.create, the revised data should be returned';
$result = Civi\Api4\Afform::update()
- ->addWhere('name', '=', 'afformExamplepage')
+ ->addWhere('name', '=', $directiveName)
->addValue('description', 'The temporary description')
->execute();
- $this->assertEquals('afformExamplepage', $result[0]['name'], $message);
+ $this->assertEquals($directiveName, $result[0]['name'], $message);
$this->assertEquals('The temporary description', $result[0]['description'], $message);
$message = 'After updating, the Afform.get API should return blended data';
- $result = Civi\Api4\Afform::get()->addWhere('name', '=', 'afformExamplepage')->execute();
- $this->assertEquals('afformExamplepage', $result[0]['name'], $message);
- $this->assertEquals('', $result[0]['title'], $message);
- $this->assertEquals('The temporary description', $result[0]['description'], $message);
- $this->assertEquals('civicrm/example-page', $result[0]['server_route'], $message);
+ $result = Civi\Api4\Afform::get()->addWhere('name', '=', $directiveName)->execute();
+ $this->assertEquals($directiveName, $result[0]['name'], $message);
+ $this->assertEquals($get($originalMetadata, 'title'), $get($result[0], 'title'), $message);
+ $this->assertEquals('The temporary description', $get($result[0], 'description'), $message);
+ $this->assertEquals($get($originalMetadata, 'server_route'), $get($result[0], 'server_route'), $message);
$this->assertTrue(is_array($result[0]['layout']), $message);
- Civi\Api4\Afform::revert()->addWhere('name', '=', 'afformExamplepage')->execute();
- $message = 'After reverting, te final Afform.get should return default data';
- $result = Civi\Api4\Afform::get()->addWhere('name', '=', 'afformExamplepage')->execute();
- $this->assertEquals('afformExamplepage', $result[0]['name'], $message);
- $this->assertEquals('', $result[0]['title'], $message);
- $this->assertEquals('', $result[0]['description'], $message);
- $this->assertEquals('civicrm/example-page', $result[0]['server_route'], $message);
+ Civi\Api4\Afform::revert()->addWhere('name', '=', $directiveName)->execute();
+ $message = 'After reverting, the final Afform.get should return default data';
+ $result = Civi\Api4\Afform::get()->addWhere('name', '=', $directiveName)->execute();
+ $this->assertEquals($directiveName, $result[0]['name'], $message);
+ $this->assertEquals($get($originalMetadata, 'title'), $get($result[0], 'title'), $message);
+ $this->assertEquals($get($originalMetadata, 'description'), $get($result[0], 'description'), $message);
+ $this->assertEquals($get($originalMetadata, 'server_route'), $get($result[0], 'server_route'), $message);
$this->assertTrue(is_array($result[0]['layout']), $message);
}