<commit_before>require 'test_helper'

class AssaysHelperTest < ActionView::TestCase
  include AssaysHelper
  include AssetsHelper

  test 'authorised_assays' do
    p1 = Factory :person
    p2 = Factory :person

    # 2 assays of the same project, but different contributors
    a1 = Factory :assay, contributor: p1, policy: Factory(:downloadable_public_policy)
    a2 = Factory :assay, study: a1.study, contributor: p2, policy: Factory(:downloadable_public_policy)

    a3 = Factory :assay, contributor: p1, policy: Factory(:downloadable_public_policy)
    a4 = Factory :assay, study: a3.study, contributor: p2, policy: Factory(:downloadable_public_policy)

    assert_equal a1.projects, a2.projects
    assert_equal a3.projects, a4.projects
    refute_equal a1.projects, a3.projects

    User.with_current_user(p1.user) do
      assays = authorised_assays(nil, 'download')
      assert_equal [a1, a2, a3, a4], assays

      assays = authorised_assays
      assert_equal [a1, a3], assays

      assays = authorised_assays(a1.projects, 'download')
      assert_equal [a1, a2], assays

      assays = authorised_assays a1.projects, 'edit'
      assert_equal [a1], assays
    end
  end
end<commit_msg>Ensure assays are sorted before checking against array<commit_after>require 'test_helper'

class AssaysHelperTest < ActionView::TestCase
  include AssaysHelper
  include AssetsHelper

  test 'authorised_assays' do
    p1 = Factory :person
    p2 = Factory :person

    # 2 assays of the same project, but different contributors
    a1 = Factory :assay, contributor: p1, policy: Factory(:downloadable_public_policy)
    a2 = Factory :assay, study: a1.study, contributor: p2, policy: Factory(:downloadable_public_policy)

    a3 = Factory :assay, contributor: p1, policy: Factory(:downloadable_public_policy)
    a4 = Factory :assay, study: a3.study, contributor: p2, policy: Factory(:downloadable_public_policy)

    assert_equal a1.projects, a2.projects
    assert_equal a3.projects, a4.projects
    refute_equal a1.projects, a3.projects

    User.with_current_user(p1.user) do
      assays = authorised_assays(nil, 'download').sort_by(&:id)
      assert_equal [a1, a2, a3, a4], assays

      assays = authorised_assays.sort_by(&:id)
      assert_equal [a1, a3], assays

      assays = authorised_assays(a1.projects, 'download').sort_by(&:id)
      assert_equal [a1, a2], assays

      assays = authorised_assays(a1.projects, 'edit').sort_by(&:id)
      assert_equal [a1], assays
    end
  end
end
{'fraction_non_alphanumeric': 0.09455128205128205, 'fraction_numerical': 0.024839743589743588, 'mean_word_length': 4.1273100616016425, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2543817', 'n_tokens_mistral': 834, 'n_tokens_neox': 793, 'n_words': 209}