Skip to content

Commit

Permalink
fixing entity test, starting on express entity builder
Browse files Browse the repository at this point in the history
  • Loading branch information
aembler committed Dec 12, 2016
1 parent c5e9138 commit df85080
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 24 deletions.
2 changes: 1 addition & 1 deletion concrete/single_pages/members/directory.php
Expand Up @@ -59,7 +59,7 @@
<?php if ($pagination->haveToPaginate()) {
?>

<?=$pagination->renderDefaultView();
<?=$pagination->renderView('dashboard');
?>

<?php
Expand Down
8 changes: 5 additions & 3 deletions concrete/src/Express/Entity/Listener.php
Expand Up @@ -49,9 +49,11 @@ public function prePersist(Entity $entity, LifecycleEventArgs $event)
if (!$entity->getEntityResultsNodeId()) {
// Create a results node
$tree = ExpressEntryResultsTree::get();
$node = $tree->getRootTreeNodeObject();
$node = ExpressEntryResultsNode::add($entity->getName(), $node);
$entity->setEntityResultsNodeId($node->getTreeNodeID());
if (is_object($tree)) {
$node = $tree->getRootTreeNodeObject();

This comment has been minimized.

Copy link
@KorvinSzanto

KorvinSzanto Dec 12, 2016

Member

What's with the tabs?

This comment has been minimized.

Copy link
@mlocati

mlocati Dec 12, 2016

Contributor

Good old tabs... I misses them

$node = ExpressEntryResultsNode::add($entity->getName(), $node);
$entity->setEntityResultsNodeId($node->getTreeNodeID());
}
}

$indexer = $entity->getAttributeKeyCategory()->getSearchIndexer();
Expand Down
104 changes: 104 additions & 0 deletions concrete/src/Express/ObjectBuilder.php
@@ -0,0 +1,104 @@
<?php
namespace Concrete\Core\Express;

use Concrete\Core\Attribute\TypeFactory;
use Concrete\Core\Entity\Attribute\Key\ExpressKey;
use Concrete\Core\Entity\Attribute\Key\Key;
use Concrete\Core\Entity\Attribute\Key\Settings\Settings;
use Concrete\Core\Entity\Express\Entity;
use Concrete\Core\Express\Attribute\AttributeKeyHandleGenerator;
use Doctrine\ORM\EntityManagerInterface;

class ObjectBuilder
{
protected $attributeTypeFactory;
protected $entity;
protected $entityManager;
protected $generator;

/**
* @return TypeFactory
*/
public function getAttributeTypeFactory()
{
return $this->attributeTypeFactory;
}

public function __construct(
AttributeKeyHandleGenerator $generator,
EntityManagerInterface $entityManager,
TypeFactory $attributeTypeFactory)
{
$this->attributeTypeFactory = $attributeTypeFactory;
$this->generator = $generator;
$this->entityManager = $entityManager;
}

public function createObject($name)
{
$this->entity = new Entity();
$this->entity->setName($name);

return $this;
}

public function __call($method, $arguments)
{
$r = call_user_func_array(array($this->entity, $method), $arguments);
if ($r !== null) {
return $r; // handle the get* methods
}
return $this; // set methods return the object builder so it can chain.
}

public function save()
{
$this->entityManager->persist($this->entity);
$this->entityManager->flush();

// grab and persist all attribute key settings object
foreach($this->entity->getAttributes() as $key) {
$settings = $key->getAttributeKeySettings();
$this->entityManager->persist($settings);
}
$this->entityManager->flush();

return $this->entity;
}

public function addAttribute($type_handle, $name, $handle = null, Settings $settings = null)
{
$key = new ExpressKey();
$key->setEntity($this->entity);
$type = $this->attributeTypeFactory->getByHandle($type_handle);
if (!is_object($settings)) {
$settings = $type->getController()->getAttributeKeySettings();
}
$settings->setAttributeKey($key);
$key->setAttributeKeySettings($settings);
$key->setAttributeKeyName($name);
$key->setAttributeType($type);
if (!$handle) {
$handle = $this->generator->generate($key);
}
$key->setAttributeKeyHandle($handle);
$this->entity->getAttributes()->add($key);
return $this;
}

public function getObject()
{
return $this->entity;
}

/**
* @return mixed
*/
public function buildObject()
{
$entity = $this->getObject();
$this->entity = null;

return $entity;
}
}
25 changes: 10 additions & 15 deletions concrete/src/Express/ObjectManager.php
@@ -1,6 +1,7 @@
<?php
namespace Concrete\Core\Express;

use Concrete\Core\Application\Application;
use Concrete\Core\Entity\Express\Entity;
use Concrete\Core\Entity\Package;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -9,9 +10,11 @@ class ObjectManager
{

protected $entityManager;
protected $app;

public function __construct(EntityManagerInterface $entityManager)
public function __construct(Application $app, EntityManagerInterface $entityManager)
{
$this->app = $app;
$this->entityManager = $entityManager;
}

Expand All @@ -30,22 +33,14 @@ public function getList($entityHandle, $asObject = false)

public function buildObject($handle, $plural_handle, $name, Package $pkg = null)
{
$entity = new Entity();
$entity->setHandle($handle);
$entity->setPluralHandle($plural_handle);
$entity->setName($name);
$builder = $this->app->make(ObjectBuilder::class);
$builder->createObject($name);
$builder->setHandle($handle);
$builder->setPluralHandle($plural_handle);
if ($pkg) {
$entity->setPackage($pkg);
$builder->setPackage($pkg);
}
return $entity;
}

public function addObject($handle, $plural_handle, $name, Package $pkg = null)
{
$entity = $this->buildObject($handle, $name, $plural_handle, $pkg);
$this->entityManager->persist($entity);
$this->entityManager->flush();
return $entity;
return $builder;
}

public function getEntry($entryID)
Expand Down
12 changes: 7 additions & 5 deletions concrete/src/Tree/Type/ExpressEntryResults.php
Expand Up @@ -66,11 +66,13 @@ public static function add()
$permissions = ['view_express_entries', 'add_express_entries', 'edit_express_entries', 'delete_express_entries'];
foreach($permissions as $handle) {
$pk = ExpressTreeNodeKey::getByHandle($handle);
$pk->setPermissionObject($rootNode);
$pa = Access::create($pk);
$pa->addListItem($adminGroupEntity);
$pt = $pk->getPermissionAssignmentObject();
$pt->assignPermissionAccess($pa);
if (is_object($pk)) {
$pk->setPermissionObject($rootNode);
$pa = Access::create($pk);
$pa->addListItem($adminGroupEntity);
$pt = $pk->getPermissionAssignmentObject();
$pt->assignPermissionAccess($pa);
}
}

return $tree;
Expand Down
Empty file modified packages/.gitignore 100644 → 100755
Empty file.
Empty file modified tests/config/concrete.php 100644 → 100755
Empty file.
Empty file modified tests/config/site.php 100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions tests/tests/Core/Express/AttributeKeyHandleGeneratorTest.php
Expand Up @@ -9,6 +9,11 @@ class AttributeKeyHandleGeneratorTest extends ConcreteDatabaseTestCase
'Concrete\Core\Entity\Attribute\Key\ExpressKey',
];

protected $tables = array(
'Trees',
'TreeTypes',
);

public function testExpressHandleGenerator()
{
$entity = new \Concrete\Core\Entity\Express\Entity();
Expand Down
151 changes: 151 additions & 0 deletions tests/tests/Core/Express/ObjectBuilderTest.php
@@ -0,0 +1,151 @@
<?php

class ObjectBuilderTest extends ConcreteDatabaseTestCase
{

protected $pkg;

protected $tables = array(
'Trees',
'TreeNodes',
'TreeGroupNodes',
'TreeTypes',
'TreeNodeTypes',
'TreeNodePermissionAssignments',
'PermissionAccessEntities',
'PermissionAccessEntityGroups',
'PermissionAccessEntityTypes',
'PermissionKeys',
'PermissionKeyCategories',
'Groups',
);

protected $metadatas = [
'Concrete\Core\Entity\Express\Entity',
'Concrete\Core\Entity\Package',
'Concrete\Core\Entity\Attribute\Type',
'Concrete\Core\Entity\Attribute\Key\ExpressKey',
'Concrete\Core\Entity\Attribute\Key\Key',
'Concrete\Core\Entity\Attribute\Key\Settings\TextSettings',
'Concrete\Core\Entity\Attribute\Key\Settings\AddressSettings',
'Concrete\Core\Entity\Attribute\Key\Settings\TextareaSettings',
];

protected function setUp()
{
parent::setUp();

\Concrete\Core\User\Group\Group::add('Administrators', '', null, null, ADMIN_GROUP_ID);

\Concrete\Core\Tree\Node\NodeType::add('category');
\Concrete\Core\Tree\Node\NodeType::add('express_entry_category');
\Concrete\Core\Tree\TreeType::add('express_entry_results');
\Concrete\Core\Tree\Node\NodeType::add('express_entry_results');

$tree = \Concrete\Core\Tree\Type\ExpressEntryResults::add();

$em = \Database::connection()->getEntityManager();
$pkg = new \Concrete\Core\Entity\Package();
$pkg->setPackageHandle('test');
$pkg->setPackageVersion('1.0');
$pkg->setPackageDescription('sigh');
$em->persist($pkg);
$em->flush();

$this->pkg = $pkg;

$factory = \Core::make('\Concrete\Core\Attribute\TypeFactory');
$factory->add('text', 'Text');
$factory->add('address', 'Address');
$factory->add('textarea', 'Textarea');
}

public function testBasicObjectBuilder()
{
$marina = Express::buildObject('marina', 'marinas', 'Marina', $this->pkg);
$this->assertInstanceOf('Concrete\Core\Express\ObjectBuilder', $marina);
$marina->setDescription('This is my marina object.');
$marina = $marina->save();

$this->assertInstanceOf('Concrete\Core\Entity\Express\Entity', $marina);
$this->assertNotEquals('', $marina->getID());
$this->assertEquals('This is my marina object.', $marina->getDescription());
$this->assertEquals('Marina', $marina->getName());
$this->assertEquals('marina', $marina->getHandle());
$this->assertEquals('marinas', $marina->getPluralHandle());
$this->assertNotNull($marina->getEntityResultsNodeId());
$this->assertInstanceOf('Concrete\Core\Entity\Package', $marina->getPackage());
}

public function testObjectBuilderWithAttributes()
{
$student = Express::buildObject('student', 'students', 'Student', $this->pkg);
$student->addAttribute('text', 'First Name');
$settings = new \Concrete\Core\Entity\Attribute\Key\Settings\TextSettings();
$settings->setPlaceholder('Last Name');
$student->addAttribute('text', 'Last Name', 'last_name', $settings);
$student = $student->save();


$category = Core::make('Concrete\Core\Attribute\Category\ExpressCategory',
['entity' => $student]);

$attributes = $category->getList();
$key1 = $attributes[0];
$key2 = $attributes[1];
$this->assertEquals(2, count($attributes));
$this->assertEquals('first_name', $key1->getAttributeKeyHandle());
$this->assertEquals('last_name', $key2->getAttributeKeyHandle());

$type = $key1->getAttributeType();
$this->assertInstanceOf('Concrete\Core\Entity\Attribute\Type', $type);
$this->assertEquals('text', $type->getAttributeTypeHandle());

$settings1 = $key1->getAttributeKeySettings();
$settings2 = $key2->getAttributeKeySettings();
$this->assertEquals('', $settings1->getPlaceholder());
$this->assertEquals('Last Name', $settings2->getPlaceholder());
}

public function testObjectBuilderWithAttributes2()
{
$student = Express::buildObject('student', 'students', 'Student', $this->pkg);
$settings = new \Concrete\Core\Entity\Attribute\Key\Settings\AddressSettings();
$settings->setCustomCountries(array("US","UK"));
$settings->setHasCustomCountries(true);
$settings->setDefaultCountry("CA");
$student->addAttribute('address', 'Address', 'address', $settings);
$student = $student->save();

$attributes = $student->getAttributes();
$key1 = $attributes[0];
$this->assertEquals('address', $key1->getAttributeKeyHandle());
$type = $key1->getAttributeType();
$this->assertInstanceOf('Concrete\Core\Entity\Attribute\Type', $type);
$this->assertEquals('address', $type->getAttributeTypeHandle());
$settings1 = $key1->getAttributeKeySettings();
$this->assertEquals(true, $settings1->hasCustomCountries());
$this->assertEquals('CA', $settings1->getDefaultCountry());
$countries = $settings1->getCustomCountries();
$this->assertEquals(2, count($countries));
}

public function testCreateObjectAndForm()
{
$student = Express::buildObject('student', 'students', 'Student', $this->pkg);
$student->addAttribute('text', 'First Name');
$student->addAttribute('text', 'Last Name');
$student->addAttribute('textarea', 'Bio');
$student->buildForm()
->addFieldset('Basics')
->addAttributeKeyControl('first_name')
->addAttributeKeyControl('last_name')
->addTextControl('', 'This is just some basic explanatory text.')
->addAttributeKeyControl('textarea')
->save();
$student->save();

}

}

0 comments on commit df85080

Please sign in to comment.