menu_link.test 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Menu link module.
  5. */
  6. /**
  7. * Tests for menu link field and formatter.
  8. */
  9. class MenuLinkFieldTestCase extends DrupalWebTestCase {
  10. protected $instance;
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Menu link field',
  14. 'description' => 'Test the creation of menu link fields.',
  15. 'group' => 'Menu link',
  16. );
  17. }
  18. function setUp() {
  19. parent::setUp('field_test');
  20. $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
  21. $this->drupalLogin($web_user);
  22. // Setup a field and instance.
  23. $this->field_name = drupal_strtolower($this->randomName());
  24. $this->field = array(
  25. 'field_name' => $this->field_name,
  26. 'type' => 'menu_link',
  27. );
  28. field_create_field($this->field);
  29. $this->instance = array(
  30. 'field_name' => $this->field_name,
  31. 'entity_type' => 'test_entity',
  32. 'bundle' => 'test_bundle',
  33. 'setings' => array(
  34. 'menu_options' => array('main-menu'),
  35. ),
  36. 'widget' => array(
  37. 'type' => 'menu_link_default',
  38. ),
  39. 'display' => array(
  40. 'full' => array(
  41. 'type' => 'menu_link_link',
  42. ),
  43. ),
  44. );
  45. field_create_instance($this->instance);
  46. }
  47. /**
  48. * Test menu link field validation.
  49. */
  50. function testMenuLinkFieldValidation() {
  51. // Test valid and invalid values with field_attach_validate().
  52. $langcode = LANGUAGE_NONE;
  53. $entity = field_test_create_stub_entity();
  54. $entity->uri = array('path' => 'test', 'options' => array());
  55. $entity->{$this->field_name}[$langcode][0] = array(
  56. 'menu_name' => 'main-menu',
  57. 'plid' => 0,
  58. 'link_title' => 'Title'
  59. );
  60. try {
  61. field_attach_validate('test_entity', $entity);
  62. $this->pass(t('Correct menu link does not cause validation error'));
  63. }
  64. catch (FieldValidationException $e) {
  65. $this->fail(t('Correct menu link does not cause validation error'));
  66. }
  67. $entity = field_test_create_stub_entity();
  68. $entity->uri = array('path' => 'test', 'options' => array());
  69. $entity->{$this->field_name}[$langcode][0] = array(
  70. 'menu_name' => 'nonexistant-menu',
  71. 'plid' => 0,
  72. 'link_title' => 'Title'
  73. );
  74. try {
  75. field_attach_validate('test_entity', $entity);
  76. $this->fail(t('Menu link with an invalid menu name causes validation error'));
  77. }
  78. catch (FieldValidationException $e) {
  79. $this->pass(t('Menu link with an invalid menu name causes validation error'));
  80. }
  81. }
  82. }