groupManager = $this->getMockBuilder('\OC\Group\Manager') ->disableOriginalConstructor() ->getMock(); $this->userSession = $this->createMock(IUserSession::class); $this->groupMetadata = new \OC\Group\MetaData( 'foo', $this->isAdmin, $this->isDelegatedAdmin, $this->groupManager, $this->userSession ); } private function getGroupMock($countCallCount = 0) { $group = $this->getMockBuilder('\OC\Group\Group') ->disableOriginalConstructor() ->getMock(); $group->expects($this->exactly(6)) ->method('getGID') ->will($this->onConsecutiveCalls( 'admin', 'admin', 'g2', 'g2', 'g3', 'g3')); $group->expects($this->exactly(3)) ->method('getDisplayName') ->will($this->onConsecutiveCalls( 'admin', 'g2', 'g3')); $group->expects($this->exactly($countCallCount)) ->method('count') ->with('') ->will($this->onConsecutiveCalls(2, 3, 5)); return $group; } public function testGet(): void { $group = $this->getGroupMock(); $groups = array_fill(0, 3, $group); $this->groupManager->expects($this->once()) ->method('search') ->with('') ->willReturn($groups); [$adminGroups, $ordinaryGroups] = $this->groupMetadata->get(); $this->assertSame(1, count($adminGroups)); $this->assertSame(2, count($ordinaryGroups)); $this->assertSame('g2', $ordinaryGroups[0]['name']); // user count is not loaded $this->assertSame(0, $ordinaryGroups[0]['usercount']); } public function testGetWithSorting(): void { $this->groupMetadata->setSorting(1); $group = $this->getGroupMock(3); $groups = array_fill(0, 3, $group); $this->groupManager->expects($this->once()) ->method('search') ->with('') ->willReturn($groups); [$adminGroups, $ordinaryGroups] = $this->groupMetadata->get(); $this->assertSame(1, count($adminGroups)); $this->assertSame(2, count($ordinaryGroups)); $this->assertSame('g3', $ordinaryGroups[0]['name']); $this->assertSame(5, $ordinaryGroups[0]['usercount']); } public function testGetWithCache(): void { $group = $this->getGroupMock(); $groups = array_fill(0, 3, $group); $this->groupManager->expects($this->once()) ->method('search') ->with('') ->willReturn($groups); //two calls, if caching fails call counts for group and groupmanager //are exceeded $this->groupMetadata->get(); $this->groupMetadata->get(); } //get() does not need to be tested with search parameters, because they are //solely and only passed to GroupManager and Group. public function testGetGroupsAsAdmin(): void { $this->groupManager ->expects($this->once()) ->method('search') ->with('Foo') ->willReturn(['DummyValue']); $expected = ['DummyValue']; $this->assertSame($expected, $this->invokePrivate($this->groupMetadata, 'getGroups', ['Foo'])); } } on value='committer'>committer
path: root/test/unit/custom_field_version_format_test.rb
blob: a4a790c1272320b703fe993eaf83ad961fca7005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Redmine - project management software
# Copyright (C) 2006-2013  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require File.expand_path('../../test_helper', __FILE__)

class CustomFieldVersionFormatTest < ActiveSupport::TestCase
  fixtures :custom_fields, :projects, :members, :users, :member_roles, :trackers, :issues, :versions

  def setup
    @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'version')
  end

  def test_possible_values_with_no_arguments
    assert_equal [], @field.possible_values
    assert_equal [], @field.possible_values(nil)
  end

  def test_possible_values_with_project_resource
    project = Project.find(1)
    possible_values = @field.possible_values(project.issues.first)
    assert possible_values.any?
    assert_equal project.shared_versions.sort.collect(&:id).map(&:to_s), possible_values
  end

  def test_possible_values_with_nil_project_resource
    assert_equal [], @field.possible_values(Issue.new)
  end

  def test_possible_values_options_with_no_arguments
    assert_equal [], @field.possible_values_options
    assert_equal [], @field.possible_values_options(nil)
  end

  def test_possible_values_options_with_project_resource
    project = Project.find(1)
    possible_values_options = @field.possible_values_options(project.issues.first)
    assert possible_values_options.any?
    assert_equal project.shared_versions.sort.map {|u| [u.name, u.id.to_s]}, possible_values_options
  end

  def test_possible_values_options_with_array
    projects = Project.find([1, 2])
    possible_values_options = @field.possible_values_options(projects)
    assert possible_values_options.any?
    assert_equal (projects.first.shared_versions & projects.last.shared_versions).sort.map {|u| [u.name, u.id.to_s]}, possible_values_options
  end

  def test_cast_blank_value
    assert_equal nil, @field.cast_value(nil)
    assert_equal nil, @field.cast_value("")
  end

  def test_cast_valid_value
    version = @field.cast_value("2")
    assert_kind_of Version, version
    assert_equal Version.find(2), version
  end

  def test_cast_invalid_value
    assert_equal nil, @field.cast_value("187")
  end
end