Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Define a menu item with an access callback and verify it is called and if it returns FALSE then accordingly menu_get_item set $access to FALSE. Define local tasks below and check MENU_DEFAULT_LOCAL_TASK properly inherits.
Comment | File | Size | Author |
---|---|---|---|
#14 | menu-296292-14.patch | 11.4 KB | bradfordcp |
#13 | menu-296292-13.patch | 11.29 KB | bradfordcp |
#11 | menu-296292-11.patch | 7.97 KB | bradfordcp |
#9 | menu-296292-9.patch | 6.72 KB | bradfordcp |
Comments
Comment #1
demeester_roel CreditAttribution: demeester_roel commentedhowto write the first test
Create a custom Module (in info set hidden = True)
implement the hook_menu and have it call an access_callback function.
Provide some content to be displayed when you would 'click' the menu
Have the access_callback return TRUE.
From within the menuCallback.test ..
call the menu page.
Check whether the result returns your own page.
Next...
have another access_callback return FALSE
From within the menuCallback.test ..
call that menu page.
Check that it returns a 404 (page not found)
Also check that $access = false
....
---------
Comment #2
mixel CreditAttribution: mixel commentedComment #3
dmitrig01 CreditAttribution: dmitrig01 commentedComment #4
mixel CreditAttribution: mixel commentedSorry, I've wanted to do this, but all the time stuff getting between it.
Comment #6
bradfordcp CreditAttribution: bradfordcp commentedWouldn't it return 403 instead of 404 as the menu item exists, you just do not have access permissions?
Comment #7
bradfordcp CreditAttribution: bradfordcp commentedI could also see this test and other callback related items within the test file created at #331720: TestingParty: Write a test for menu callbacks in different files
Comment #8
bradfordcp CreditAttribution: bradfordcp commentedComment #9
bradfordcp CreditAttribution: bradfordcp commentedComment #10
pwolanin CreditAttribution: pwolanin commentedsubscribe so I rememebr to review
Comment #11
bradfordcp CreditAttribution: bradfordcp commentedA new patch! Adds code from #296293: TestingParty08: menu access callback default need a test (mikey_p)
Comment #12
bradfordcp CreditAttribution: bradfordcp commentedCurrently does not take care of "Define local tasks below and check MENU_DEFAULT_LOCAL_TASK properly inherits." in the issue description.
Comment #13
bradfordcp CreditAttribution: bradfordcp commentedComment #14
bradfordcp CreditAttribution: bradfordcp commentedFixed a menu test that was comparing the results of drupalGet. Now it stores and compares the values of $this->content after each call.
Comment #15
sunMissing trailing dot. Also, this description, in general, makes me nervous.
...missing trailing dots all over the place.
Please shorten the message, i.e. "MENU_NORMAL_ITEM is displayed." is sufficient.
There is no actual testing of the "menu path request result" here.
Same here - a result of 200 can mean anything.
testMenuLocalTaskTypes() is the only valid test.
No need for such a comment. It's obvious from the next line anyway.
I don't understand what "local" means in all these strings here. Cruft?
On that note, there does not seem to be a test that verifies the menu item's title is displayed.
Empty arrays don't need to be defined.
This definition is actually wrong - I really wonder why the testbot does not complain.
http://api.drupal.org/api/function/page_example_perm/7 luckily contains a valid example.
We should check for an actual permission - or even better, use some custom access check - here. TRUE means nothing, it's the default in most cases.
To properly test for access denied, the menu item should use a non-existent permission. I.e. no 'access callback', but according 'access arguments'. When using such code (i.e. a non-existing permission), a code comment should clarify what's done there.
Since tests are ran by uid 1 by default, the test needs to create and log in a separate user for this. (Something I miss in these tests anyway.) Bear in mind that uid 1 can access each and everything.
Why do we introduce a separate .test and .module file for menu callback tests?
Last, but not least, there is trailing white-space on lines that should be empty.
Comment #16
bradfordcp CreditAttribution: bradfordcp commentedWow, that is a little intense. I will look into the areas mentioned and make the improvements, but could you expand on a few points?
Do you mean that I am not checking that when the link is followed it goes to the right page? I can see that the only thing being tested is that the link appears.
In this case I guess it should check the content of the page, but 200 doesn't mean "anything". I was verifying that it didn't 404 while trying to reach the page.
Local stands for a function that is within the file with hook_menu() in it. You may also notice that there are similarly named functions which use 'include' instead of 'local' as the functions being called are within the .inc file. I believe it helps provide a visual distinction between the locations of the functions. Looking at the comments I apparently made a copy/paste error but if you look at these functions you can see the test.
I was looking at http://api.drupal.org/api/function/hook_perm/7 for proper syntax. If it needs to be in the format as specified by the page example module, I will update it.
The whole purpose here is to verify that the function gets called, should this be
return user_access('some perm');
In this instance we are returning FALSE, not even uid 1 can access the page when the access callback returns false (if it could the test wouldn't pass ;) ). What you are describing as a solution is test elsewhere, take a look at:
I just thought they didn't fit with the other menu tests, thus I moved them into their own. All of these tests are very targeted to the menu callback subsystem. Looking at the menu test code it appears as though Drupal generates the proper trees and such. Should these tests be cobbled together with menu.module and menu.test?
Thanks for your help, I will fix a bunch of these and push out another patch.
Comment #17
catchMoving this out of the critical bugs queue - see #607038: Meta issue: fix gaps in code coverage.
Comment #18
gowriabhaya CreditAttribution: gowriabhaya commentedCode sprint tag