diff options
Diffstat (limited to 'test/models/node_test.rb')
| -rw-r--r-- | test/models/node_test.rb | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/test/models/node_test.rb b/test/models/node_test.rb index c8e49e5..3fb2d23 100644 --- a/test/models/node_test.rb +++ b/test/models/node_test.rb | |||
| @@ -761,4 +761,104 @@ class NodeTest < ActiveSupport::TestCase | |||
| 761 | assert_equal "Second", action.metadata.dig("title", "from") | 761 | assert_equal "Second", action.metadata.dig("title", "from") |
| 762 | assert_equal "First", action.metadata.dig("title", "to") | 762 | assert_equal "First", action.metadata.dig("title", "to") |
| 763 | end | 763 | end |
| 764 | |||
| 765 | test "default_template_name rejects values not present in the template directory" do | ||
| 766 | node = Node.root.children.build(:slug => "template_guard_test", | ||
| 767 | :default_template_name => "../../layouts/admin") | ||
| 768 | assert_not node.valid? | ||
| 769 | |||
| 770 | node.default_template_name = "standard_template" | ||
| 771 | assert node.valid? | ||
| 772 | end | ||
| 773 | |||
| 774 | test "destroying a node with children is refused" do | ||
| 775 | parent = Node.root.children.create!(:slug => "destroy_guard_parent") | ||
| 776 | parent.children.create!(:slug => "destroy_guard_child") | ||
| 777 | |||
| 778 | assert_no_difference "Node.count" do | ||
| 779 | assert_not parent.destroy | ||
| 780 | end | ||
| 781 | assert parent.errors[:base].any? | ||
| 782 | end | ||
| 783 | |||
| 784 | test "destroying a childless node leaves no orphaned pages or asset links" do | ||
| 785 | node = create_node_with_published_page | ||
| 786 | page_ids = node.pages.pluck(:id) | ||
| 787 | asset = Asset.create!(:name => "destroy cascade probe", | ||
| 788 | :upload_file_name => "test_image.png", | ||
| 789 | :upload_content_type => "image/png", | ||
| 790 | :upload_file_size => 49854, | ||
| 791 | :upload_updated_at => Time.current) | ||
| 792 | node.head.related_assets.create!(:asset_id => asset.id, :position => 1) | ||
| 793 | |||
| 794 | node.destroy | ||
| 795 | |||
| 796 | assert_equal 0, Page.where(:id => page_ids).count | ||
| 797 | assert_equal 0, RelatedAsset.where(:page_id => page_ids).count | ||
| 798 | end | ||
| 799 | |||
| 800 | test "destroying a node also removes its autosave page" do | ||
| 801 | node = create_node_with_published_page | ||
| 802 | node.lock_for_editing!(@user1) | ||
| 803 | node.autosave!({:title => "in flight"}, @user1) | ||
| 804 | autosave_id = node.autosave_id | ||
| 805 | |||
| 806 | node.destroy | ||
| 807 | |||
| 808 | assert_equal 0, Page.where(:id => autosave_id).count | ||
| 809 | end | ||
| 810 | |||
| 811 | test "Node.trash lazily creates the container exactly once" do | ||
| 812 | assert_difference "Node.count", 1 do | ||
| 813 | Node.trash | ||
| 814 | end | ||
| 815 | assert_no_difference "Node.count" do | ||
| 816 | assert_equal Node.trash, Node.trash | ||
| 817 | end | ||
| 818 | assert Node.trash.trash_node? | ||
| 819 | assert_not Node.trash.in_trash? | ||
| 820 | end | ||
| 821 | |||
| 822 | test "in_trash? walks the whole parent chain" do | ||
| 823 | child = Node.trash.children.create!(:slug => "trashed_thing") | ||
| 824 | grandchild = child.children.create!(:slug => "trashed_deeper") | ||
| 825 | |||
| 826 | assert child.in_trash? | ||
| 827 | assert grandchild.in_trash? | ||
| 828 | assert_not Node.root.children.create!(:slug => "living_thing").in_trash? | ||
| 829 | end | ||
| 830 | |||
| 831 | test "the reserved slug is refused for other root children, live and staged" do | ||
| 832 | Node.trash | ||
| 833 | |||
| 834 | assert_not Node.root.children.build(:slug => CccConventions::TRASH_SLUG).valid? | ||
| 835 | assert_not Node.root.children.build(:slug => "fine", :staged_slug => CccConventions::TRASH_SLUG).valid? | ||
| 836 | assert Node.trash.children.create!(:slug => "sub").children.build(:slug => CccConventions::TRASH_SLUG).valid? | ||
| 837 | end | ||
| 838 | |||
| 839 | test "the Trash node refuses rename, move, and destroy" do | ||
| 840 | trash = Node.trash | ||
| 841 | other = Node.root.children.create!(:slug => "not_the_trash") | ||
| 842 | |||
| 843 | trash.slug = "recycling" | ||
| 844 | assert_not trash.valid? | ||
| 845 | |||
| 846 | trash.reload.parent_id = other.id | ||
| 847 | assert_not trash.valid? | ||
| 848 | |||
| 849 | assert_no_difference "Node.count" do | ||
| 850 | assert_not trash.reload.destroy | ||
| 851 | end | ||
| 852 | end | ||
| 853 | |||
| 854 | test "a head cannot exist inside the Trash, and publishing there is refused" do | ||
| 855 | node = Node.trash.children.create!(:slug => "no_publish_here") | ||
| 856 | page = node.pages.create! | ||
| 857 | |||
| 858 | node.head = page | ||
| 859 | assert_not node.valid? | ||
| 860 | |||
| 861 | node.reload | ||
| 862 | assert_raises(ActiveRecord::RecordInvalid) { node.publish_draft! } | ||
| 863 | end | ||
| 764 | end | 864 | end |
