Testing Namespaced Controllers - Filters not run (Sanity check)

36 views
Skip to first unread message

Adam Chapman

unread,
May 23, 2013, 8:06:46 PM5/23/13
to cfwh...@googlegroups.com
Hi All,

I am trying to write a functional test for a simple CRUD controller which is in a subfolder (ColdRoute namespaced).

I have run into an issue where testing a namespaced controller won't run the controller filters..

Eg.. Controller Snippet:

<cfcomponent extends="Controller" output="false">
<cffunction name="init">
<cfset filters(through="getOwner")>
</cffunction>

<cffunction name="index">
<cfset categories = model("Category").findAll(where="ownerid = #owner.ownerid#")>
</cffunction>

</cfcomponent>

Functional Test (Note controller="admin.categories"):

<cffunction name="test_index_displays_category_listing">
    <!--- setup some params for the tests --->
    <cfset loc.params = {controller="admin.categories", action="index"}>
    <!--- create an instance of the controller --->
    <cfset loc.controller = controller("admin.categories", loc.params)>
    <!--- process the action of the controller --->
    <cfset loc.controller.$processAction()>
    <!--- get copy of the code the view generated --->
    <cfset loc.response = loc.controller.response()>
    <!--- make sure this string is displayed  --->
    <cfset loc.string = '<h2>Categories</h2>'>
    <cfset assert('loc.response contains loc.string')>
</cffunction>

The test result suggests that the getOwner filter is not being run.

CategoriesTest  INDEX DISPLAYS CATEGORY LISTING Error
variable [owner] doesn't exist
Categories.cfc line 28

When I move the Categories controller out of the /controllers/admin and back into the root /controllers folder.. the test passes and all is rosey!

Has anyone else got this combination working correctly? Could this be a wheels or coldroute bug?

Some more info:
- Railo 4.0.4.001
- OSX
- controllers/admin/Categories.cfc extends controllers/admin/Controller.cfc which extends /controllers/Controller.cfc
- get getOwner function is in the /controllers/Controller.cfc
- only the Categories.cfc has an init method
- I have also tried controller="admin/categories" in the test
- If I hack my controller and call getOwner() in the index function, the test passes.

Any thoughts appreciated.
Regards,
Adam


Donald Humphreys

unread,
May 24, 2013, 8:44:05 AM5/24/13
to cfwh...@googlegroups.com
Adam,


It is very unlikely that this issue is caused directly by ColdRoute,
as it is mainly a high level way to define routes. There is some
modified route matching logic, but again, that is separate from the
controller processing side of Wheels. There are also some patches that
were applied to Wheels to make the namespaced controller names work
(admin.categories), but I am not sure how that would affect the
running of filters at all. The company I developed ColdRoute for is
running literally hundreds of namespaced controllers with several
filters and no issues. If your issue is isolated to functional
testing, I would look into the way you are invoking the
controller/action and make sure it isn't skipping the filter
invocation logic in Wheels.

Can anyone else recreate this issue? Was the latest filter logic
written to handle the dot syntax in controller names? I don't imagine
that it would make a difference as long as the controller object is
properly constructed.

Let me know if I can be of additional assistance.

Don
> --
> You received this message because you are subscribed to the Google Groups
> "ColdFusion on Wheels" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cfwheels+u...@googlegroups.com.
> To post to this group, send email to cfwh...@googlegroups.com.
> Visit this group at http://groups.google.com/group/cfwheels?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Adam Chapman

unread,
May 24, 2013, 7:12:44 PM5/24/13
to cfwh...@googlegroups.com
Thanks Don.. I will investigate the controller call from the test framework and post my findings.

Adam Chapman

unread,
May 29, 2013, 9:44:24 PM5/29/13
to cfwh...@googlegroups.com
After some testing (thanks Tony) it appears that the controller's init() function is not called.. therefore the filters are not run.

This ONLY occurs when the controller is in a subfolder..

Works:
<cfset loc.controller = controller("categories", loc.params)>

Errors:
<cfset loc.controller = controller("admin.categories", loc.params)>

A workaround is to explicitly call the init function.

<cffunction name="test_index_displays_category_listing">
    <cfset loc.params = {controller="admin.categories", action="index"}>
    <cfset loc.controller = controller("admin.categories", loc.params)>
    <cfset loc.controller.init()> <!--- workaround --->
    ....
</cffunction>
Reply all
Reply to author
Forward
0 new messages