summaryrefslogtreecommitdiffstats
path: root/test/ui/sudo_mode_test_ui.rb
blob: 2fbaa31fd1a6e162093882204e3e7a136c8f2339 (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
# Redmine - project management software
# Copyright (C) 2006-2015  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('../base', __FILE__)

class Redmine::UiTest::SudoModeTest < Redmine::UiTest::Base
  fixtures :users, :email_addresses

  def setup
    Redmine::SudoMode.stubs(:enabled?).returns(true)
  end

  def test_add_user
    log_user('admin', 'admin')
    visit '/users/new'

    assert_difference 'User.count' do
      within('form#new_user') do
        fill_in 'Login', :with => 'johnpaul'
        fill_in 'First name', :with => 'John'
        fill_in 'Last name', :with => 'Paul'
        fill_in 'Email', :with => 'john@example.net'
        fill_in 'Password', :with => 'password'
        fill_in 'Confirmation', :with => 'password'
        # click_button 'Create' would match both 'Create' and 'Create and continue' buttons
        find('input[name=commit]').click
      end

      assert_equal '/users', current_path
      assert page.has_content?("Confirm your password to continue")
      assert page.has_css?('form#sudo-form')

      within('form#sudo-form') do
        fill_in 'Password', :with => 'admin'
        click_button 'Submit'
      end
    end
  end
end
old; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.complexscripts.bidi;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.fop.layoutmgr.BidiUtil;
import org.apache.fop.text.bidi.BidiClassUtils;
import org.apache.fop.util.BidiConstants;

/**
 * <p>Test case for Unicode Bidi Algorithm.</p>
 * @author Glenn Adams
 */
public class BidiAlgorithmTestCase extends TestCase {

    /**
     * logging instance
     */
    private static final Log log = LogFactory.getLog(BidiAlgorithmTestCase.class);                                      // CSOK: ConstantNameCheck

    /**
     * Concatenated array of <test-set,test-sequence> tuples
     * specifying which sequences are to be excluded from testing,
     * where -1 for either component is a wildcard.
     */
    private static final int[] EXCLUSIONS = {
        // no exclusions
    };

    /**
     * Concatenated array of <test-set,test-sequence> tuples
     * specifying which sequences are to be included in testing, where
     * -1 for either component is a wildcard.
     */
    private static final int[] INCLUSIONS = {
        -1, -1                                  // all sequences
    };

    /**
     * Concatenated array of <start,end> tuples expressing ranges of
     * test sets to be tested, where -1 in the end position signifies
     * all remaining test sets.
     */
    private static final int[] TEST_SET_RANGES = {
        0, -1                                   // all test sets
    };

    // instrumentation 
    private int includedSequences;
    private int excludedSequences;
    private int passedSequences;

    public void testBidiAlgorithm() throws Exception {
        String ldPfx = BidiTestData.LD_PFX;
        int ldCount = BidiTestData.LD_CNT;
        for ( int i = 0; i < ldCount; i++ ) {
            int[] da = BidiTestData.readTestData ( ldPfx, i );
            if ( da != null ) {
                testBidiAlgorithm ( i, da );
            } else {
                fail ( "unable to read bidi test data for resource at index " + i );
            }
        }
        // ensure we passed all test sequences
        assertEquals ( "did not pass all test sequences", BidiTestData.NUM_TEST_SEQUENCES, passedSequences );
        if ( log.isDebugEnabled() ) {
            log.debug ( "Included Sequences : " + includedSequences );
            log.debug ( "Excluded Sequences : " + excludedSequences );
            log.debug( "Passed Sequences   : " + passedSequences );
        }
    }

    private void testBidiAlgorithm ( int testSet, int[] da ) throws Exception {
        if ( da.length < 1 ) {
            fail ( "test data is empty" );
        } else if ( da.length < ( ( da[0] * 2 ) + 1 ) ) {
            fail ( "test data is truncated" );
        } else {
            int k = 0;
            // extract level count
            int n = da[k++];
            // extract level array
            int[] la = new int [ n ];
            for ( int i = 0; i < n; i++ ) {
                la[i] = da[k++];
            }
            // extract reorder array
            int[] ra = new int [ n ];
            for ( int i = 0; i < n; i++ ) {
                ra[i] = da[k++];
            }
            // extract and test each test sequence
            int testSequence = 0;
            int[] ta = new int [ n ];
            while ( ( k + ( 1 + n ) ) <= da.length ) {
                int bs = da[k++];
                for ( int i = 0; i < n; i++ ) {
                    ta[i] = da[k++];
                }
                if ( includeSequence ( testSet, testSequence ) ) {
                    includedSequences++;
                    if ( ! excludeSequence ( testSet, testSequence ) ) {
                        if ( testBidiAlgorithm ( testSet, testSequence, la, ra, ta, bs ) ) {
                            passedSequences++;
                        }
                    } else {
                        excludedSequences++;
                    }
                }
                testSequence++;
            }
            // ensure we exhausted test data
            assertEquals ( "extraneous test data", da.length, k );
        }
    }

    private boolean includeTestSet ( int testSet ) {
        for ( int i = 0, n = TEST_SET_RANGES.length / 2; i < n; i++ ) {
            int s = TEST_SET_RANGES [ ( i * 2 ) + 0 ];
            int e = TEST_SET_RANGES [ ( i * 2 ) + 1 ];
            if ( testSet >= s ) {
                if ( ( e < 0 ) || ( testSet <= e ) ) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean includeSequence ( int testSet, int testSequence ) {
        if ( ! includeTestSet ( testSet ) ) {
            return false;
        } else {
            for ( int i = 0, n = INCLUSIONS.length / 2; i < n; i++ ) {
                int setno = INCLUSIONS [ ( i * 2 ) + 0 ];
                int seqno = INCLUSIONS [ ( i * 2 ) + 1 ];
                if ( setno < 0 ) {
                    if ( seqno < 0 ) {
                        return true;
                    } else if ( seqno == testSequence ) {
                        return true;
                    }
                } else if ( setno == testSet ) {
                    if ( seqno < 0 ) {
                        return true;
                    } else if ( seqno == testSequence ) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    private boolean excludeSequence ( int testSet, int testSequence ) {
        for ( int i = 0, n = EXCLUSIONS.length / 2; i < n; i++ ) {
            int setno = EXCLUSIONS [ ( i * 2 ) + 0 ];
            int seqno = EXCLUSIONS [ ( i * 2 ) + 1 ];
            if ( setno < 0 ) {
                if ( seqno < 0 ) {
                    return true;
                } else if ( seqno == testSequence ) {
                    return true;
                }
            } else if ( setno == testSet ) {
                if ( seqno < 0 ) {
                    return true;
                } else if ( seqno == testSequence ) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean testBidiAlgorithm ( int testSet, int testSequence, int[] la, int[] ra, int[] ta, int bs ) throws Exception {
        boolean passed = true;
        int n = la.length;
        if ( ra.length != n ) {
            fail ( "bad reorder array length, expected " + n + ", got " + ra.length );
        } else if ( ta.length != n ) {
            fail ( "bad test array length, expected " + n + ", got " + ta.length );
        } else {
            // auto-LTR
            if ( ( bs & 1 ) != 0 ) {
                // auto-LTR is performed at higher level
            }
            // LTR
            if ( ( bs & 2 ) != 0 ) {
                int[] levels = BidiUtil.UnicodeBidiAlgorithm.resolveLevels ( null, ta, 0, new int [ n ], true );
                if ( ! verifyResults ( la, levels, ta, 0, testSet, testSequence ) ) {
                    passed = false;
                }
            }
            // RTL
            if ( ( bs & 4 ) != 0 ) {
                int[] levels = BidiUtil.UnicodeBidiAlgorithm.resolveLevels ( null, ta, 1, new int [ n ], true );
                if ( ! verifyResults ( la, levels, ta, 1, testSet, testSequence ) ) {
                    passed = false;
                }
            }
        }
        return passed;
    }

    private boolean verifyResults ( int[] laExp, int[] laOut, int[] ta, int dl, int testSet, int testSequence ) {
        if ( laOut.length != laExp.length ) {
            fail ( "output levels array length mismatch, expected " + laExp.length + ", got " + laOut.length );
            return false;
        } else {
            int numMatch = 0;
            for ( int i = 0, n = laExp.length; i < n; i++ ) {
                if ( laExp[i] >= 0 ) {
                    int lo = laOut[i];
                    int le = laExp[i];
                    if ( lo != le ) {
                        assertEquals ( getMismatchMessage ( testSet, testSequence, i, dl ), le, lo );
                    } else {
                        numMatch++;
                    }
                } else {
                    numMatch++;
                }
            }
            return numMatch == laExp.length;
        }
    }

    private String getMismatchMessage ( int testSet, int testSequence, int seqIndex, int defaultLevel ) {
        StringBuffer sb = new StringBuffer();
        sb.append ( "level mismatch for default level " );
        sb.append ( defaultLevel );
        sb.append ( " at sequence index " );
        sb.append ( seqIndex );
        sb.append ( " in test sequence " );
        sb.append ( testSequence );
        sb.append ( " of test set " );
        sb.append ( testSet );
        return sb.toString();
    }

}